【发布时间】:2015-02-13 00:20:36
【问题描述】:
我试图让右上角的正方形与它下面的两个正方形的宽度相同,我想让下面的 JTextArea 也匹配宽度。有什么想法吗?
似乎无论我将大小设置为什么,它都在做它想做的事情。例如,Output JTextArea 设置为只有一列。上图为 (700x250),两个半图为 (350x250)。
public class MyApplet extends Applet{
private static final long serialVersionUID = 1L;
private JTextArea input_data;
private JTextArea input_jmax;
private JTextArea input_gibbs;
private JTextArea input_burnin;
private JTextArea output_text;
private JLabel output_graph;
private JLabel output_burn1;
private JLabel output_burn2;
private static Graphics g=null;
public void init () {
//INPUT
this.input_data = new JTextArea("Enter Data", 30, 30);
JScrollPane data_pane= new JScrollPane(input_data);
this.input_jmax = new JTextArea("Polya-Tree Levels", 1, 30);
this.input_gibbs = new JTextArea("Gibbs Iterates", 1, 30);
this.input_burnin = new JTextArea("Burnin", 1, 30);
//OUTPUT
Dimension D;
D = new Dimension(700, 250);
Image start;
this.output_text = new JTextArea("####################Output####################",15,1);
this.output_text.setEditable(false);
JScrollPane output_pane= new JScrollPane(output_text);
this.output_burn1 = new JLabel();
D = new Dimension(345,250);
start = createImage((int) D.getWidth(), (int) D.getHeight());
g = start.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0,0,345,250);
output_burn1.setIcon(new ImageIcon(start));
this.output_burn2 = new JLabel();
D = new Dimension(345, 250);
start = createImage((int) D.getWidth(), (int) D.getHeight());
g = start.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0,0,345,250);
output_burn2.setIcon(new ImageIcon(start));
this.output_graph = new JLabel();
start = createImage((int) D.getWidth(), (int) D.getHeight());
g = start.getGraphics();
g.setColor(Color.WHITE);
g.fillRect(0,0,700,250);
output_graph.setIcon(new ImageIcon(start));
//BUTTON
JButton b = new JButton("Process Data");
//set size
setSize(1200, 600);
setBackground(Color.lightGray);
JPanel burninPanel = new JPanel();
burninPanel.setLayout(new BoxLayout(burninPanel, BoxLayout.X_AXIS));
burninPanel.add(output_burn1);
burninPanel.add(Box.createHorizontalStrut(10));
burninPanel.add(output_burn2);
/*
//Create Input Side
* */
JPanel inputPanel = new JPanel();
inputPanel.setLayout(new BoxLayout(inputPanel, BoxLayout.Y_AXIS));
inputPanel.setBackground(Color.LIGHT_GRAY);
inputPanel.add(data_pane);
inputPanel.add(Box.createVerticalStrut(10));
inputPanel.add(input_jmax);
inputPanel.add(Box.createVerticalStrut(10));
inputPanel.add(input_gibbs);
inputPanel.add(Box.createVerticalStrut(10));
inputPanel.add(input_burnin);
inputPanel.add(Box.createVerticalStrut(10));
inputPanel.add(b);
//Create Output Side
JPanel outputPanel = new JPanel();
outputPanel.setBackground(Color.lightGray);
outputPanel.setLayout(new BoxLayout(outputPanel, BoxLayout.Y_AXIS));
outputPanel.add(output_graph);
outputPanel.add(Box.createVerticalStrut(10));
outputPanel.add(burninPanel);
outputPanel.add(Box.createVerticalStrut(10));
outputPanel.add(output_pane);
this.setLayout(new GridLayout(1, 2));
this.setVisible(true);
GroupLayout layout = new GroupLayout(this);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
layout.setHorizontalGroup(layout.createSequentialGroup()
.addGroup(layout.createSequentialGroup()
.addComponent(inputPanel)
.addComponent(outputPanel)
)
);
layout.setVerticalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup()
.addComponent(inputPanel)
.addComponent(outputPanel)
)
);
this.setLayout(layout);
this.setVisible(true);
// specify that action events sent by the
// button or the input TextField should be handled
// by the same CapitalizerAction object
Multiplicity ca = new Multiplicity(input_data, input_jmax, input_gibbs, input_burnin, output_text,output_graph);
b.addActionListener(ca);
//this.input.addActionListener(ca);
}
}
【问题讨论】:
-
创建一个自定义面板,您可以在其上绘制图表。使用适当的布局管理器将其添加到您的小程序中。您可能会考虑在 21 世纪与我们其他人一起使用,至少考虑使用 Swing 而不是 AWT,哦等等,您正在混合 AWT 和 Swing 组件......这会很好地结束......
-
要知道,AWT 和 Swing 组件之间是有区别的,AWT 组件在 Swing 组件上表现不佳,最好避免混合使用;)
-
这样更好吗?您知道如何使用组布局获取第二列的任何更改吗?困难的是网格的大小不一样......我什至是在吠叫正确的树吗? @MadProgrammer
-
你3个核心区,左、中、右。自行布置每个区域
JPanel。完成后,您可以使用GridLayout或GridBagLayout将这三个面板添加到主 UI -
好的!我们已经开始使用组布局中的摇摆元素运行。我仍然有同样的对齐问题,除了现在文本区域之间也没有填充。 @MadProgrammer
标签: java swing jpanel layout-manager grouplayout