gpt4 book ai didi

Java GridBagLayout定位

转载 作者:行者123 更新时间:2023-12-01 18:00:57 25 4
gpt4 key购买 nike

我正在尝试使用 GridBagLayout 创建一个窗口,这是我的代码:

   import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class ReadMessage extends JFrame implements ActionListener
{
JButton Last;
JButton Delete;
JButton Store;
JButton Next;
JTextArea MessageBox;

public ReadMessage()
{
setLayout(new FlowLayout());
JPanel Panel = new JPanel();
add(Panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Panel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();


MessageBox = new JTextArea();
MessageBox.setEditable(false);
JScrollPane scrollPane = new JScrollPane(MessageBox, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
MessageBox.setLineWrap(true);
c.gridx = 0;
c.gridy = 0;
c.gridwidth = 4;
c.weightx = 0.0;
c.ipady = 300;
c.ipadx = 300;
Panel.add(scrollPane, c);


Last = new JButton("Last");
c.gridx = 0;
c.gridy = 1;
c.ipady = 0;
c.weightx = 0.5;
Panel.add(Last, c);
Last.addActionListener(this);

Delete = new JButton("Delete");
c.gridx = 1;
c.gridy = 1;
c.ipady = 0;
c.weightx = 0.5;
Panel.add(Delete, c);
Delete.addActionListener(this);

Store = new JButton("Store");
c.gridx = 2;
c.gridy = 1;
c.ipady = 0;
c.weightx = 0.5;
Panel.add(Store, c);
Store.addActionListener(this);

Next = new JButton("Next");
c.gridx = 3;
c.gridy = 1;
c.ipady = 0;
c.weightx = 0.5;
Panel.add(Next, c);
Next.addActionListener(this);

}



}

结果是这样的 enter image description here

我真正想要的就是这样

enter image description here

我知道我做错了,但我不知道我到底做错了什么,我阅读了oracle上的文档,但找不到任何东西,你能指出我做错了什么,以及如何修复它吗?非常感谢

最佳答案

所有按钮的网格宽度均为 4,而不是 1。

关于Java GridBagLayout定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40954266/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com