我正在从事“各种各样的工作”而不是学校作业。话虽如此,任何想法都会很棒。
我在Eclipse中使用WindowBuilder并创建了一个基本表单。我在边框布局上使用了嵌套的JPanel
组件来创建它。出于某种原因,面板看起来好像溢出了JFrame的边缘。我将框架尺寸设置为(500,400),面板尺寸各异,但宽度不超过400。
代码:
package SwingAssignment;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import java.awt.GridBagLayout;
import javax.swing.BoxLayout;
import net.miginfocom.swing.MigLayout;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import java.awt.FlowLayout;
import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import com.jgoodies.forms.factories.FormFactory;
import java.awt.GridLayout;
import javax.swing.JLabel;
import javax.swing.border.BevelBorder;
import javax.swing.SwingConstants;
public class Swing_Assignemnt {
private JFrame frmWindowBuilderAssignment;
private JPanel Center_Panel;
private JTextArea textArea;
private JPanel panel_1;
private JPanel panel;
private JTextField textField;
private JPanel panel_2;
private JTextArea txtrTextarea_0;
private JTextArea txtrTextarea_1;
private JPanel panel_3;
private JTextArea txtrTextareasouth;
private JLabel lblNewLabel;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Swing_Assignemnt window = new Swing_Assignemnt();
window.frmWindowBuilderAssignment.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Swing_Assignemnt() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frmWindowBuilderAssignment = new JFrame();
frmWindowBuilderAssignment.setTitle("Window Builder Assignment");
//frmWindowBuilderAssignment.setBounds(500, 500, 650, 600);
frmWindowBuilderAssignment.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frmWindowBuilderAssignment.getContentPane().setLayout(new BorderLayout(0, 0));
frmWindowBuilderAssignment.setVisible(true);
frmWindowBuilderAssignment.setSize(394, 500);
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
//frmWindowBuilderAssignment.pack();
//frmWindowBuilderAssignment.pack();
frmWindowBuilderAssignment.setVisible( true );
panel = new JPanel();
panel.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
frmWindowBuilderAssignment.getContentPane().add(panel, BorderLayout.NORTH);
panel.setPreferredSize(new Dimension(200, 40));
panel.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
//panel.setBounds(20, 10, 200, 400);
panel.setVisible(true);
JComboBox comboBox = new JComboBox();
panel.add(comboBox);
comboBox.setPreferredSize(new Dimension(125, 20));
comboBox.setVisible(true);
textField = new JTextField();
panel.add(textField);
textField.setColumns(10);
textField.setVisible(true);
panel_2 = new JPanel();
panel_2.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
frmWindowBuilderAssignment.getContentPane().add(panel_2, BorderLayout.CENTER);
panel_2.setLayout(new GridLayout(1, 2, 2, 2));
panel_2.setPreferredSize(new Dimension(200, 400));
panel_2.setVisible(true);
txtrTextarea_0 = new JTextArea();
txtrTextarea_0.setText("textArea_0");
panel_2.add(txtrTextarea_0);
txtrTextarea_0.setPreferredSize(new Dimension(50, 30));
txtrTextarea_0.setVisible(true);
txtrTextarea_1 = new JTextArea();
txtrTextarea_1.setText("textArea_1");
panel_2.add(txtrTextarea_1);
txtrTextarea_1.setVisible(true);
txtrTextarea_1.setPreferredSize(new Dimension(50, 30));
panel_3 = new JPanel();
panel_3.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
frmWindowBuilderAssignment.getContentPane().add(panel_3, BorderLayout.SOUTH);
panel_3.setLayout(new GridLayout(2, 1, 2, 2));
panel_3.setVisible(true);
txtrTextareasouth = new JTextArea();
txtrTextareasouth.setText("textArea_South");
panel_3.add(txtrTextareasouth);
txtrTextareasouth.setVisible(true);
txtrTextareasouth.setPreferredSize(new Dimension(200, 150));
lblNewLabel = new JLabel("Status Label");
panel_3.add(lblNewLabel);
lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
lblNewLabel.setPreferredSize(new Dimension(200, 20));
}
}
它看起来像什么:
经过一些代码更改后,这就是我现在所拥有的,我不知道如何在底部调整标签大小。它只需要大约15个高。
答案 0 :(得分:3)
不要使用setBounds()或setPreferredSize()。每个Swing组件应确定自己的首选大小,然后布局管理器将根据布局管理器的规则定位组件。
不要在所有Swing组件上使用setVisible(true)(JFrame除外)。默认情况下,Swing组件是可见的。
在使用之前,您应该将所有组件添加到框架中:
frame.pack()
frame.setVisible( true );
答案 1 :(得分:0)
尝试使用
frmWindowBuilderAssignment.pack();
在初始化方法结束时。
这会将所有组件设置为首选大小并调整框架。