紧急求助(毕业设计用):GUI设计的显示问题,多谢

colin719 2004-04-27 11:06:34
我的源程序如下:在显示的的时候,A,B,C,D等几个按钮没有均匀分布,按照原本的设想,他们应该是均匀分布的,我看源程序又没有发现什么问题,那位大侠可以帮助小弟一下,等着急用.多谢了。
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JEditorPane;
import javax.swing.JScrollPane;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import java.awt.BorderLayout;
import javax.swing.BoxLayout;
import java.awt.Component;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.FlowLayout;
import java.awt.Container;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class AllInOne
extends JFrame {
private JRadioButton[] engines = {new JRadioButton("A", true), new JRadioButton("B", false),
new JRadioButton("C", false), new JRadioButton("D", false),
new JRadioButton("E", false), new JRadioButton("F", false),
new JRadioButton("G", false), new JRadioButton("H", false)};
private GridBagLayout gbLayout;
private GridBagConstraints gbConstraints;
private Container container;

public AllInOne() {
container = getContentPane();
gbLayout = new GridBagLayout();
container.setLayout(gbLayout);
gbConstraints = new GridBagConstraints();
JLabel tips = new JLabel("Please select your answer: ");
gbConstraints.anchor=GridBagConstraints.WEST;
addComponent(tips,0,0,GridBagConstraints.NONE,1);
JTextField input = new JTextField("input here",30);
addComponent(input,1,0,GridBagConstraints.HORIZONTAL,GridBagConstraints.RELATIVE);
gbConstraints.anchor=GridBagConstraints.CENTER;
JButton search = new JButton("GoTo");
addComponent(search,0,0,GridBagConstraints.NONE,GridBagConstraints.REMAINDER);
ButtonGroup radioGroup = new ButtonGroup();
gbConstraints.anchor=GridBagConstraints.WEST;
for(int i=0; i<engines.length-1; i++){
radioGroup.add(engines[i]);
addComponent(engines[i],0,0,GridBagConstraints.NONE,i+1);
}
radioGroup.add(engines[engines.length-1]);
addComponent(engines[engines.length-1],0,0,GridBagConstraints.NONE,GridBagConstraints.REMAINDER);
JEditorPane contents = new JEditorPane();
contents.setEditable(false);
gbConstraints.anchor=GridBagConstraints.CENTER;
addComponent(new JScrollPane(contents),0,1,GridBagConstraints.BOTH,GridBagConstraints.REMAINDER);
this.setSize(800,600);
this.show();
}
private void addComponent(Component c,double weightx,double weighty, int fill,int gridwidth){
gbConstraints.weightx=weightx;//可水平分配的多余空间部分。当存在多余空间时,构件可以变得更宽。默认值是0。
gbConstraints.weighty=weighty;//可垂直分配的多余空间部分。当存在多余空间时,构件可以变得更高。默认值是0。
gbConstraints.fill=fill;//指定所占据的构件区域的大小。默认值是NONE.
gbConstraints.gridwidth=gridwidth;//构件占据的行数。
gbLayout.setConstraints(c,gbConstraints);
container.add(c);
}

public static void main(String[] args) {
AllInOne win = new AllInOne();
win.addWindowListener(
new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
);
}

}
...全文
58 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
ecaol 2004-04-27
  • 打赏
  • 举报
回复
帮你UP
ecaol 2004-04-27
  • 打赏
  • 举报
回复
怎么用了这么多import用个import javax.swing.*;不就都搞定了吗
..先帮你顶一下

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧