首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java相关 >

为何图形会不一样

2013-07-01 
为什么图形会不一样package FiveTeenimport javax.swing.*import java.awt.*public class Exercise15_2

为什么图形会不一样

package FiveTeen;

import javax.swing.*;
import java.awt.*;

public class Exercise15_2 extends JFrame {
// Create two buttons
private OvalButton jbtOk = new OvalButton("OK");
private OvalButton jbtCancel = new OvalButton("Cancel");

/** Default constructor */
public Exercise15_2() {
// Set FlowLayout manager to arrange the components
// inside the frame
setLayout(new FlowLayout());

// Add buttons to the frame
add(jbtOk);
add(jbtCancel);
}

/** Main method */
public static void main(String[] args) {
Exercise15_2 frame = new Exercise15_2();
frame.setTitle("Exercise15_2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(100, 80);
frame.setLocationRelativeTo(null); // Center the frame
frame.setVisible(true);
}
}

class OvalButton extends JButton {
public OvalButton() {
}

public OvalButton(String text) {
super(text);
}

protected void paintComponent(Graphics g) {
// Draw an oval
super.paintComponent(g);
g.drawOval(5, 5, getWidth() - 10, getHeight() - 10);
}

/** Override get method for preferredSize */
public Dimension getPreferredSize() {
return new Dimension(100, 50);
}

/** Override get method for minimumSize */
public Dimension getMinimumSize() {
return new Dimension(100, 50);
}
}

这是最先的代码,运行后两个按钮的大小是一样的
package FiveTeen;

import javax.swing.*;
import java.awt.*;

public class Exercise15_2 extends JFrame {
// Create two buttons
private OvalButton jbtOk = new OvalButton("OK");
private OvalButton jbtCancel = new OvalButton("Cancel");

/** Default constructor */
public Exercise15_2() {
// Set FlowLayout manager to arrange the components
// inside the frame
setLayout(new FlowLayout());

// Add buttons to the frame
add(jbtOk);
add(jbtCancel);
}

/** Main method */
public static void main(String[] args) {
Exercise15_2 frame = new Exercise15_2();
frame.setTitle("Exercise15_2");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(100, 80);
frame.setLocationRelativeTo(null); // Center the frame
frame.setVisible(true);
}
}

class OvalButton extends JButton {
public OvalButton() {
}

public OvalButton(String text) {
super(text);
}

protected void paintComponent(Graphics g) {
// Draw an oval
super.paintComponent(g);
g.drawOval(5, 5, getWidth() - 10, getHeight() - 10);
}

///** Override get method for preferredSize */
//public Dimension getPreferredSize() {


//return new Dimension(100, 50);
//}
//
///** Override get method for minimumSize */
//public Dimension getMinimumSize() {
//return new Dimension(100, 50);
//}
}


将最下面两个方法屏蔽掉后,在运行时,发现两个按钮大小变得不一样了,求解 图形 Java
[解决办法]
那两个方法就是设置按钮尺寸的,去掉后按钮大小就自动适应文本了。
我猜测是这样,呵呵。

热点排行