我正在开发一个程序。它的GUI有两个主要部分,JFrame的左边和JFrame的右边。 (目前右半部分是空白的,因为我还没有开始工作)。关于GUI的布局
左侧部分看起来不太好。所有的按钮和文本框都被拉伸。我希望他们拥有标准按钮的高度,与本网站上的按钮相似。 (你知道,标准的Windows按钮)。
我该怎么做? (我不想简单地打包()整个事情,因为窗口的右半部分会有一个大的方形JPanel,所以pack()ing意味着窗口仍然是方形的而左半边的按钮仍然会被拉长和拉下)。
这里有一个画面:
下面是到目前为止的代码:
import javax.swing.*;
import java.awt.*;
import java.awt.Event.*;
public class GUI extends JFrame {
JButton rect,oval,tri,free,addPoint;
JLabel xLabel,yLabel;
JTextField xTextField,yTextField;
JPanel leftPanel,rightPanel,optionsPanel,pointsPanel;
public GUI(){
initUI();
}
private void initUI(){
setLayout(new GridLayout(1,2,5,5));
setDefaultCloseOperation(EXIT_ON_CLOSE);
setTitle("Graphics Generator");
setSize(500,500);
rect = new JButton("Rectangle");
oval = new JButton("Oval");
tri = new JButton("Triangle");
free = new JButton("Free Shape");
addPoint = new JButton("Add point");
xLabel = new JLabel("X: ");
yLabel = new JLabel("Y: ");
xTextField = new JTextField(2);
yTextField = new JTextField(2);
leftPanel = new JPanel();
rightPanel = new JPanel();
optionsPanel = new JPanel();
pointsPanel = new JPanel();
add(leftPanel);
add(rightPanel);
leftPanel.setLayout(new GridLayout(2,1,5,5));
leftPanel.add(optionsPanel);
optionsPanel.setLayout(new GridLayout(1,4,2,2));
optionsPanel.add(rect);
optionsPanel.add(oval);
optionsPanel.add(tri);
optionsPanel.add(free);
leftPanel.add(pointsPanel);
pointsPanel.setLayout(new GridLayout(1,5,2,2));
pointsPanel.add(xLabel);
pointsPanel.add(xTextField);
pointsPanel.add(yLabel);
pointsPanel.add(yTextField);
pointsPanel.add(addPoint);
setVisible(true);
}
public static void main(String[] args) {
GUI gui = new GUI();
}
}
问题也许是你所选择的布局。尝试使用不同的布局并检查结果? – ujvl
你应该添加一个草图,说明你想要完成的gui的样子。只是一个线框素描,没有什么奇特的。 – Paaske