Java GridBagLayout Swing中的JSpinner和JButton
这是创建我的消息面板的代码:Java GridBagLayout Swing中的JSpinner和JButton,java,swing,jbutton,layout-manager,jspinner,Java,Swing,Jbutton,Layout Manager,Jspinner,这是创建我的消息面板的代码: private class MessagePane extends JPanel { private MessagePane() { setLayout(new GridBagLayout()); setBackground(backgroundColor); GridBagConstraints gbc = new GridBagConstraints(); gbc.gridx = 0;
private class MessagePane extends JPanel {
private MessagePane() {
setLayout(new GridBagLayout());
setBackground(backgroundColor);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(4, 4, 4, 4);
add(allowButton, gbc);
gbc.gridx = 1;
add(blockButton, gbc);
gbc.gridwidth = 2;
gbc.gridx = 0;
gbc.gridy = 1;
gbc.weightx = 1;
gbc.weighty = 1;
add(timerL, gbc);
gbc.fill = GridBagConstraints.BOTH;
add(new JScrollPane(messageArea), gbc);
gbc = new GridBagConstraints();
gbc.gridy = 2;
gbc.gridx = 0;
add(countryList, gbc);
gbc.gridx = 1;
add(msgList, gbc);
gbc.gridx = 2;
add(sendButton, gbc);
setVisible(true);
}
}
这个视图看起来像:
我想实现的观点是:
你能帮我解决这个问题吗?你没有正确设置gbc约束宽度,但是已经说过,对于这个GUI,我不会使用GridBagLayout。我想:
- 对整个JPanel使用BorderLayout
- 使用GridLayout将顶部按钮添加到JPanel中,GridLayout有1行2列,中间有一些空格
- 将该JPanel放入主JPanel
BorderLayout.PAGE\u START
- 放置中心组件
BorderLayout.center
- 使用沿直线(而不是页面)定向的BoxLayout为底部创建JPanel
- 将其添加到主JPanel
BorderLayout.PAGE\u END
- 对整个JPanel使用BorderLayout
- 使用GridLayout将顶部按钮添加到JPanel中,GridLayout有1行2列,中间有一些空格
- 将该JPanel放入主JPanel
BorderLayout.PAGE\u START
- 放置中心组件
BorderLayout.center
- 使用沿直线(而不是页面)定向的BoxLayout为底部创建JPanel
- 将其添加到主JPanel
BorderLayout.PAGE\u END
- 绿色边界区域:1个柱跨度
- 红色边界区域:2列跨度
- 蓝色边界区域:4列跨度李>
- 绿色边界区域:1个柱跨度
- 红色边界区域:2列跨度
- 蓝色边界区域:4列跨度李>
如果需要更多帮助,请发布一个可编译的示例。我使用
GridBagLayout
创建了以下GUI
我必须把最上面的两个按钮放在它们自己的JPanel中,这样它们的大小就相同了。我对按钮面板和主面板都使用了GridBagLayout
我为每个Swing组件创建了一个GridBagConstraints
。这样,我可以按照自己的意愿设置插图,并为每个组件指定不同的锚定和填充
我不得不猜测OP使用了哪些Swing组件,因为他们没有在他发布的代码中定义
下面是生成GUI的完整、可运行的示例
import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.SpinnerListModel;
import javax.swing.SwingUtilities;
public class MessageApplication implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new MessageApplication());
}
@Override
public void run() {
JFrame frame = new JFrame("Message Panel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new MessagePanel().getPanel());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public class MessagePanel {
private JPanel panel;
public MessagePanel() {
// top, left, bottom, right
Insets topLeftInsets = new Insets(10, 10, 10, 10);
Insets topInsets = new Insets(10, 0, 10, 10);
Insets leftInsets = new Insets(0, 10, 10, 10);
Insets insets = new Insets(0, 0, 10, 10);
Insets messageInsets = new Insets(2, 4, 2, 4);
Insets zeroInsets = new Insets(0, 0, 0, 0);
panel = new JPanel();
panel.setLayout(new GridBagLayout());
int gridy = 0;
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridBagLayout());
JButton allowButton = new JButton("ALLOW");
addComponent(buttonPanel, allowButton, 0, gridy, 1, 1,
topLeftInsets, GridBagConstraints.CENTER,
GridBagConstraints.HORIZONTAL);
JButton blockButton = new JButton("BLOCK");
addComponent(buttonPanel, blockButton, 1, gridy, 1, 1,
topInsets, GridBagConstraints.CENTER,
GridBagConstraints.HORIZONTAL);
addComponent(panel, buttonPanel, 0, gridy++, 6, 1,
zeroInsets, GridBagConstraints.CENTER,
GridBagConstraints.HORIZONTAL);
JTextArea messageArea = new JTextArea(12, 30);
messageArea.setEditable(false);
messageArea.setMargin(messageInsets);
messageArea.setText("I'm a message area.");
JScrollPane scrollPane =
new JScrollPane(messageArea);
addComponent(panel, scrollPane, 0, gridy++, 6, 1,
leftInsets, GridBagConstraints.CENTER,
GridBagConstraints.HORIZONTAL);
String[] countryStrings = { "US", "CA" };
SpinnerListModel countryModel =
new SpinnerListModel(countryStrings);
JSpinner countryList = new JSpinner(countryModel);
addComponent(panel, countryList, 0, gridy, 1, 1,
leftInsets, GridBagConstraints.CENTER,
GridBagConstraints.HORIZONTAL);
String[] msgStrings = { "Message to the client",
"Message to the shipper" };
SpinnerListModel msgModel =
new SpinnerListModel(msgStrings);
JSpinner msgList = new JSpinner(msgModel);
addComponent(panel, msgList, 1, gridy, 4, 1,
insets, GridBagConstraints.CENTER,
GridBagConstraints.HORIZONTAL);
JButton sendButton = new JButton("SEND");
addComponent(panel, sendButton, 5, gridy++, 1, 1,
insets, GridBagConstraints.CENTER,
GridBagConstraints.HORIZONTAL);
}
private void addComponent(Container container,
Component component, int gridx, int gridy,
int gridwidth, int gridheight, Insets insets,
int anchor, int fill) {
GridBagConstraints gbc = new GridBagConstraints(
gridx, gridy, gridwidth, gridheight,
1.0, 1.0, anchor, fill, insets, 0, 0);
container.add(component, gbc);
}
public JPanel getPanel() {
return panel;
}
}
}
我使用
GridBagLayout
创建了以下GUI
我必须把最上面的两个按钮放在它们自己的JPanel中,这样它们的大小就相同了。我对按钮面板和主面板都使用了GridBagLayout
我为每个Swing组件创建了一个GridBagConstraints
。这样,我可以按照自己的意愿设置插图,并为每个组件指定不同的锚定和填充
我不得不猜测OP使用了哪些Swing组件,因为他们没有在他发布的代码中定义
下面是生成GUI的完整、可运行的示例
import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.SpinnerListModel;
import javax.swing.SwingUtilities;
public class MessageApplication implements Runnable {
public static void main(String[] args) {
SwingUtilities.invokeLater(new MessageApplication());
}
@Override
public void run() {
JFrame frame = new JFrame("Message Panel");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new MessagePanel().getPanel());
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public class MessagePanel {
private JPanel panel;
public MessagePanel() {
// top, left, bottom, right
Insets topLeftInsets = new Insets(10, 10, 10, 10);
Insets topInsets = new Insets(10, 0, 10, 10);
Insets leftInsets = new Insets(0, 10, 10, 10);
Insets insets = new Insets(0, 0, 10, 10);
Insets messageInsets = new Insets(2, 4, 2, 4);
Insets zeroInsets = new Insets(0, 0, 0, 0);
panel = new JPanel();
panel.setLayout(new GridBagLayout());
int gridy = 0;
JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(new GridBagLayout());
JButton allowButton = new JButton("ALLOW");
addComponent(buttonPanel, allowButton, 0, gridy, 1, 1,
topLeftInsets, GridBagConstraints.CENTER,
GridBagConstraints.HORIZONTAL);
JButton blockButton = new JButton("BLOCK");
addComponent(buttonPanel, blockButton, 1, gridy, 1, 1,
topInsets, GridBagConstraints.CENTER,
GridBagConstraints.HORIZONTAL);
addComponent(panel, buttonPanel, 0, gridy++, 6, 1,
zeroInsets, GridBagConstraints.CENTER,
GridBagConstraints.HORIZONTAL);
JTextArea messageArea = new JTextArea(12, 30);
messageArea.setEditable(false);
messageArea.setMargin(messageInsets);
messageArea.setText("I'm a message area.");
JScrollPane scrollPane =
new JScrollPane(messageArea);
addComponent(panel, scrollPane, 0, gridy++, 6, 1,
leftInsets, GridBagConstraints.CENTER,
GridBagConstraints.HORIZONTAL);
String[] countryStrings = { "US", "CA" };
SpinnerListModel countryModel =
new SpinnerListModel(countryStrings);
JSpinner countryList = new JSpinner(countryModel);
addComponent(panel, countryList, 0, gridy, 1, 1,
leftInsets, GridBagConstraints.CENTER,
GridBagConstraints.HORIZONTAL);
String[] msgStrings = { "Message to the client",
"Message to the shipper" };
SpinnerListModel msgModel =
new SpinnerListModel(msgStrings);
JSpinner msgList = new JSpinner(msgModel);
addComponent(panel, msgList, 1, gridy, 4, 1,
insets, GridBagConstraints.CENTER,
GridBagConstraints.HORIZONTAL);
JButton sendButton = new JButton("SEND");
addComponent(panel, sendButton, 5, gridy++, 1, 1,
insets, GridBagConstraints.CENTER,
GridBagConstraints.HORIZONTAL);
}
private void addComponent(Container container,
Component component, int gridx, int gridy,
int gridwidth, int gridheight, Insets insets,
int anchor, int fill) {
GridBagConstraints gbc = new GridBagConstraints(
gridx, gridy, gridwidth, gridheight,
1.0, 1.0, anchor, fill, insets, 0, 0);
container.add(component, gbc);
}
public JPanel getPanel() {
return panel;
}
}
}
在GridBagLayout中定义6列。顶部按钮各跨3列,中间文本区域跨所有6列,微调器跨1列和4列。“发送”按钮跨越最后一列。@GilbertLeBlanc:请将其作为答案张贴在GridBagLayout中的6列中。顶部按钮各跨3列,中间文本区域跨所有6列,微调器跨1列和4列。“发送”按钮跨越最后一列。@GilbertLeBlanc:请将其作为答案发布