我使用GridLayout mananger创建我的gui,但我不喜欢额外的空间让我的按钮看起来很小。 My GUI
我希望它看起来更像这个。 Good GUI
我尝试将前两行放在另一个gridlayout中并将它们放在流布局中,但它没有用。它们最终会并排,除非窗口变得非常小,因为它应该是一个流程布局。任何想法?
标准服务小组构造函数
public StandardServices()
{
// Create GridLayout manager with 5 rows and 1 column
setLayout(new GridLayout(5,1));
// Create the check boxes.
iHardDrive = new JCheckBox("Install Hard Drive ($25.00)");
ram = new JCheckBox("Install Ram ($15.00)");
virus = new JCheckBox("Remove Virus ($50.00)");
fHardDrive = new JCheckBox("Format Hard Drive ($80.00)");
labourQuote = new JCheckBox("Hourly Labour Qoute ($10.00)");
//Add a border around the panel.
setBorder(BorderFactory.createTitledBorder("Standard Services"));
// Add the checkboxes to the panel.
add(iHardDrive);
add(ram);
add(virus);
add(fHardDrive);
add(labourQuote);
}
每小时服务面板构造函数
public HourlyService()
{
// Created grid layout with 2 rows, 1 column
setLayout(new GridLayout(2,1));
// Create labels to display instructions.
cost = new JLabel("Parts Cost:");
labour = new JLabel("Hours of Labour:");
// Create two text fields 10 characters wide.
costTextField = new JTextField(10);
labourTextField = new JTextField(10);
// Place a 0 in the text fields.
costTextField.setText("0");
labourTextField.setText("0");
// Add a border around the layout
setBorder(BorderFactory.createTitledBorder("Hourly Service"));
// Add labels and text fields to the panel.
add(cost);
add(costTextField);
add(labour);
add(labourTextField);
}
LU Store GUI构造函数
public MyStoreGui()
{
//Display a title
setTitle("LU Computer Store");
//Specify a default action for the close button
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Set the size of the window
setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
// Create a GridLayout manager
setLayout(new GridLayout(3,1));
//setLayout(new FlowLayout());
// create custom panels
standard = new StandardServices();
hourly = new HourlyService();
//Create the button panel
buildButtonPanel();
add(standard);
add(hourly);
add(buttonPanel);
// Display the window
setVisible(true);
}
答案 0 :(得分:1)
使用GridLayout
代替使用基于最大组件为所有组件提供相等间距的GridBagLayout
,而不仅仅为import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
JFrame frame = new JFrame();
frame.add(new MyStoreGui());
frame.pack();
frame.setVisible(true);
}
});
}
public final class MyStoreGui extends JPanel {
public MyStoreGui() {
setLayout(new GridBagLayout());
//setLayout(new FlowLayout());
// create custom panels
StandardServices standard = new StandardServices();
HourlyService hourly = new HourlyService();
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.fill = GridBagConstraints.HORIZONTAL;
//Create the button panel
JPanel buttonPanel = buildButtonPanel();
add(standard, gbc);
add(hourly, gbc);
add(buttonPanel, gbc);
// Display the window
setVisible(true);
}
protected JPanel buildButtonPanel() {
JPanel panel = new JPanel();
panel.add(new JButton("Calclate Changes"));
panel.add(new JButton("Exit"));
return panel;
}
}
public class StandardServices extends JPanel {
public StandardServices() {
// Create GridLayout manager with 5 rows and 1 column
setLayout(new GridLayout(5, 1));
// Create the check boxes.
JCheckBox iHardDrive = new JCheckBox("Install Hard Drive ($25.00)");
JCheckBox ram = new JCheckBox("Install Ram ($15.00)");
JCheckBox virus = new JCheckBox("Remove Virus ($50.00)");
JCheckBox fHardDrive = new JCheckBox("Format Hard Drive ($80.00)");
JCheckBox labourQuote = new JCheckBox("Hourly Labour Qoute ($10.00)");
//Add a border around the panel.
setBorder(BorderFactory.createTitledBorder("Standard Services"));
// Add the checkboxes to the panel.
add(iHardDrive);
add(ram);
add(virus);
add(fHardDrive);
add(labourQuote);
}
}
public class HourlyService extends JPanel {
public HourlyService() {
// Created grid layout with 2 rows, 1 column
setLayout(new GridLayout(2, 1));
// Create labels to display instructions.
JLabel cost = new JLabel("Parts Cost:");
JLabel labour = new JLabel("Hours of Labour:");
// Create two text fields 10 characters wide.
JTextField costTextField = new JTextField(10);
JTextField labourTextField = new JTextField(10);
// Place a 0 in the text fields.
costTextField.setText("0");
labourTextField.setText("0");
// Add a border around the layout
setBorder(BorderFactory.createTitledBorder("Hourly Service"));
// Add labels and text fields to the panel.
add(cost);
add(costTextField);
add(labour);
add(labourTextField);
}
}
}
提供更多控制权,而且会尊重首选各个组件的大小
DATA MK_RETURN_DATA;
SET MK_RETURN;
output;
RUN;
答案 1 :(得分:0)
使用方法pack()删除不必要的空格。 在最后一行使用它。