java 的一段代码,运行后控件不显示,执行最小化或者最大化操作后控件才显示,求解!!谢谢!!
packagecom.lzw;importjava.awt.*;importjavax.swing.*;publicclassUseCase1extendsJFrame{...
package com.lzw;
import java.awt.*;
import javax.swing.*;
public class UseCase1 extends JFrame{
private JComboBox comboBox;
public static void main(String[] args) {
new UseCase1();
}
public UseCase1() {
super();
setVisible(true);
setSize(200,150);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
comboBox = new JComboBox();
comboBox.addItem("红");
comboBox.addItem("黄");
comboBox.addItem("蓝");
comboBox.addItem("黑");
getContentPane().add(comboBox, BorderLayout.NORTH);
final JPanel panel = new JPanel();
getContentPane().add(panel, BorderLayout.CENTER);
final JCheckBox checkBox = new JCheckBox();
checkBox.setText("男");
panel.add(checkBox);
final JCheckBox checkBox_1 = new JCheckBox();
checkBox_1.setText("女");
panel.add(checkBox_1);
final JPanel panel_1 = new JPanel();
getContentPane().add(panel_1, BorderLayout.SOUTH);
final JButton okButton = new JButton();
okButton.setText("确定");
panel_1.add(okButton);
final JButton button_1 = new JButton();
button_1.setText("取消");
panel_1.add(button_1);
}
} 展开
import java.awt.*;
import javax.swing.*;
public class UseCase1 extends JFrame{
private JComboBox comboBox;
public static void main(String[] args) {
new UseCase1();
}
public UseCase1() {
super();
setVisible(true);
setSize(200,150);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
comboBox = new JComboBox();
comboBox.addItem("红");
comboBox.addItem("黄");
comboBox.addItem("蓝");
comboBox.addItem("黑");
getContentPane().add(comboBox, BorderLayout.NORTH);
final JPanel panel = new JPanel();
getContentPane().add(panel, BorderLayout.CENTER);
final JCheckBox checkBox = new JCheckBox();
checkBox.setText("男");
panel.add(checkBox);
final JCheckBox checkBox_1 = new JCheckBox();
checkBox_1.setText("女");
panel.add(checkBox_1);
final JPanel panel_1 = new JPanel();
getContentPane().add(panel_1, BorderLayout.SOUTH);
final JButton okButton = new JButton();
okButton.setText("确定");
panel_1.add(okButton);
final JButton button_1 = new JButton();
button_1.setText("取消");
panel_1.add(button_1);
}
} 展开
展开全部
把对JFrame类的设置移动到构造函数的末尾就行了。
具体的完整程序如下:(注意注释)
package com.lzw;
import java.awt.*;
import javax.swing.*;
public class UseCase1 extends JFrame{
private JComboBox comboBox;
public static void main(String[] args) {
new UseCase1();
}
public UseCase1() {
super();
comboBox = new JComboBox();
comboBox.addItem("红");
comboBox.addItem("黄");
comboBox.addItem("蓝");
comboBox.addItem("黑");
getContentPane().add(comboBox, BorderLayout.NORTH);
final JPanel panel = new JPanel();
getContentPane().add(panel, BorderLayout.CENTER);
final JCheckBox checkBox = new JCheckBox();
checkBox.setText("男");
panel.add(checkBox);
final JCheckBox checkBox_1 = new JCheckBox();
checkBox_1.setText("女");
panel.add(checkBox_1);
final JPanel panel_1 = new JPanel();
getContentPane().add(panel_1, BorderLayout.SOUTH);
final JButton okButton = new JButton();
okButton.setText("确定");
panel_1.add(okButton);
final JButton button_1 = new JButton();
button_1.setText("取消");
panel_1.add(button_1);
setVisible(true); //把这句移动到这里
setSize(200,150); //把这句移动到这里
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); //把这句移动到这里
}
}
具体的完整程序如下:(注意注释)
package com.lzw;
import java.awt.*;
import javax.swing.*;
public class UseCase1 extends JFrame{
private JComboBox comboBox;
public static void main(String[] args) {
new UseCase1();
}
public UseCase1() {
super();
comboBox = new JComboBox();
comboBox.addItem("红");
comboBox.addItem("黄");
comboBox.addItem("蓝");
comboBox.addItem("黑");
getContentPane().add(comboBox, BorderLayout.NORTH);
final JPanel panel = new JPanel();
getContentPane().add(panel, BorderLayout.CENTER);
final JCheckBox checkBox = new JCheckBox();
checkBox.setText("男");
panel.add(checkBox);
final JCheckBox checkBox_1 = new JCheckBox();
checkBox_1.setText("女");
panel.add(checkBox_1);
final JPanel panel_1 = new JPanel();
getContentPane().add(panel_1, BorderLayout.SOUTH);
final JButton okButton = new JButton();
okButton.setText("确定");
panel_1.add(okButton);
final JButton button_1 = new JButton();
button_1.setText("取消");
panel_1.add(button_1);
setVisible(true); //把这句移动到这里
setSize(200,150); //把这句移动到这里
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); //把这句移动到这里
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询