java图形用户界面,本人代码效果如下,但是行间距有点大,按钮之类的组件过大,怎么解决?
classFrame1extendsJFrameimplementsActionListener{JTextFieldtext_name;String[]JComboBo...
class Frame1 extends JFrame implements ActionListener{
JTextField text_name;
String[] JComboBoxItem={"JComboBoxItem"};
JComboBox aJComboBox;
JRadioButton aJRadioButton[];
JCheckBox aJCheckBox[];
JTextArea aTextArea;
JList aJList;
JSpinner aJSpinner;
JButton aJButton;
public Frame1(){
super("javax.swing包中基本组件的形状");
this.setBounds(100,100,500,600);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(new GridLayout(5,1));
//第一行
JPanel jpanel1=new JPanel();
jpanel1.setLayout(new GridLayout(1,3));
jpanel1.add(new JLabel("JLabel:",SwingConstants.RIGHT));
text_name=new JTextField(5);
jpanel1.add(text_name);
aJComboBox=new JComboBox(JComboBoxItem);
jpanel1.add(aJComboBox);
this.add(jpanel1);
//第二行
JPanel jpanel2=new JPanel();
jpanel2.setLayout(new GridLayout(1,3));
jpanel2.add(new JLabel("JCheckBox:",SwingConstants.RIGHT));
aJCheckBox=new JCheckBox[2];
aJCheckBox[0]=new JCheckBox("JCheckBox1");
aJCheckBox[1]=new JCheckBox("JCheckBox2");
jpanel2.add(aJCheckBox[0]);
jpanel2.add(aJCheckBox[1]);
this.add(jpanel2);
//第三行
JPanel jpanel3=new JPanel();
jpanel3.setLayout(new GridLayout(1,3));
jpanel3.add(new JLabel("JRadioButton:",SwingConstants.RIGHT));
ButtonGroup aButtonGroup=new ButtonGroup();
aJRadioButton=new JRadioButton[3];
aJRadioButton[0]=new JRadioButton("JRadioButton1");
aJRadioButton[1]=new JRadioButton("JRadioButton2");
aJRadioButton[2]=new JRadioButton("JRadioButton3");
aButtonGroup.add(aJRadioButton[0]);
aButtonGroup.add(aJRadioButton[1]);
aButtonGroup.add(aJRadioButton[2]);
jpanel3.add(aJRadioButton[0]);
jpanel3.add(aJRadioButton[1]);
jpanel3.add(aJRadioButton[2]);
this.add(jpanel3);
//第四行
JPanel jpanel4=new JPanel();
jpanel4.setLayout(new GridLayout(1,3));
jpanel4.add(new JLabel("JTextArea In JScrollPane:",SwingConstants.RIGHT));
aTextArea=new JTextArea(5,10);
JScrollPane scrollpane=new JScrollPane(aTextArea);
jpanel4.add(scrollpane);
jpanel4.add(new JLabel("JList:",SwingConstants.RIGHT));
String[] informationOfList={"one","two","three"};
aJList=new JList(informationOfList);
jpanel4.add(aJList);
this.add(jpanel4);
//第五行
JPanel jpanel5=new JPanel();
jpanel5.setLayout(new GridLayout(1,3));
jpanel5.add(new JLabel("JSpinner:",SwingConstants.RIGHT));
aJSpinner=new JSpinner();
jpanel5.add(aJSpinner);
aJButton=new JButton("JButton");
jpanel5.add(aJButton);
this.add(jpanel5);
this.setVisible(true);
} 展开
JTextField text_name;
String[] JComboBoxItem={"JComboBoxItem"};
JComboBox aJComboBox;
JRadioButton aJRadioButton[];
JCheckBox aJCheckBox[];
JTextArea aTextArea;
JList aJList;
JSpinner aJSpinner;
JButton aJButton;
public Frame1(){
super("javax.swing包中基本组件的形状");
this.setBounds(100,100,500,600);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setLayout(new GridLayout(5,1));
//第一行
JPanel jpanel1=new JPanel();
jpanel1.setLayout(new GridLayout(1,3));
jpanel1.add(new JLabel("JLabel:",SwingConstants.RIGHT));
text_name=new JTextField(5);
jpanel1.add(text_name);
aJComboBox=new JComboBox(JComboBoxItem);
jpanel1.add(aJComboBox);
this.add(jpanel1);
//第二行
JPanel jpanel2=new JPanel();
jpanel2.setLayout(new GridLayout(1,3));
jpanel2.add(new JLabel("JCheckBox:",SwingConstants.RIGHT));
aJCheckBox=new JCheckBox[2];
aJCheckBox[0]=new JCheckBox("JCheckBox1");
aJCheckBox[1]=new JCheckBox("JCheckBox2");
jpanel2.add(aJCheckBox[0]);
jpanel2.add(aJCheckBox[1]);
this.add(jpanel2);
//第三行
JPanel jpanel3=new JPanel();
jpanel3.setLayout(new GridLayout(1,3));
jpanel3.add(new JLabel("JRadioButton:",SwingConstants.RIGHT));
ButtonGroup aButtonGroup=new ButtonGroup();
aJRadioButton=new JRadioButton[3];
aJRadioButton[0]=new JRadioButton("JRadioButton1");
aJRadioButton[1]=new JRadioButton("JRadioButton2");
aJRadioButton[2]=new JRadioButton("JRadioButton3");
aButtonGroup.add(aJRadioButton[0]);
aButtonGroup.add(aJRadioButton[1]);
aButtonGroup.add(aJRadioButton[2]);
jpanel3.add(aJRadioButton[0]);
jpanel3.add(aJRadioButton[1]);
jpanel3.add(aJRadioButton[2]);
this.add(jpanel3);
//第四行
JPanel jpanel4=new JPanel();
jpanel4.setLayout(new GridLayout(1,3));
jpanel4.add(new JLabel("JTextArea In JScrollPane:",SwingConstants.RIGHT));
aTextArea=new JTextArea(5,10);
JScrollPane scrollpane=new JScrollPane(aTextArea);
jpanel4.add(scrollpane);
jpanel4.add(new JLabel("JList:",SwingConstants.RIGHT));
String[] informationOfList={"one","two","three"};
aJList=new JList(informationOfList);
jpanel4.add(aJList);
this.add(jpanel4);
//第五行
JPanel jpanel5=new JPanel();
jpanel5.setLayout(new GridLayout(1,3));
jpanel5.add(new JLabel("JSpinner:",SwingConstants.RIGHT));
aJSpinner=new JSpinner();
jpanel5.add(aJSpinner);
aJButton=new JButton("JButton");
jpanel5.add(aJButton);
this.add(jpanel5);
this.setVisible(true);
} 展开
3个回答
展开全部
GridLayout布局中的每个格子的组件都会跟格子一样大,不管你怎么设置都没用,嫌大了就把窗口设小点 ,PACK()可以直接让窗口以每个子面板的首选大小之和显示
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在 setVisible(true) 前加上一句 pack()
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
.装个 Netbeans
拖拽式的界面编程... 你想怎么放就怎么放
拖拽式的界面编程... 你想怎么放就怎么放
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询