
我試過使用GridBagLayout. 它使JTextArea父容器占據很大的寬度,但文本區域本身并沒有填充寬度。為什么?
我希望第一個按鈕靠近頂部,第二個按鈕靠近底部。我該如何實施?
你能告訴我如何更正我的代碼嗎?
以下代碼為演示的全部代碼。
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
public class Demo extends JFrame {
public Demo() throws HeadlessException {
add(new MainPanel());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setVisible(true);
}
class MainPanel extends JPanel {
private final InputComponent inputComponent = new InputComponent();
private final ButtonsContainer buttons = new ButtonsContainer();
public MainPanel() {
setPreferredSize(new Dimension(800, 100));
setLayout(new GridBagLayout());
setBorder(new EmptyBorder(5, 5, 5, 5));
add(inputComponent,
buildConstraints(0, 0, 1, 1, GridConstraints.FILL_BOTH, 1.0, 1.0));
add(buttons,
buildConstraints(1, 0, 1, 1, GridConstraints.FILL_BOTH, 0.0, 1.0));
}
}
class InputComponent extends JComponent {
private final JTextArea textArea = new JTextArea();
public InputComponent() {
setLayout(new BorderLayout());
textArea.setLineWrap(true);
add(textArea, BorderLayout.CENTER);
}
}
class ButtonsContainer extends JComponent {
private final JButton button1 = new JButton("Button 1");
private final JButton button2 = new JButton("Button 2");
public ButtonsContainer() {
setLayout(new GridBagLayout());
add(button1, buildConstraints(0, 0, 1, 1, GridBagConstraints.HORIZONTAL, 1.0, 0.0));
add(button2, buildConstraints(0, 1, 1, 1, GridBagConstraints.HORIZONTAL, 1.0, 0.0));
}
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
final Demo demo = new Demo();
});
}
private GridBagConstraints buildConstraints(
int x,
int y,
int gWidth,
int gHeight,
int fill,
double weightX,
double weightY
) {
final GridBagConstraints constraints = new GridBagConstraints();
constraints.gridx = x;
constraints.gridy = y;
constraints.gridwidth = gWidth;
constraints.gridheight = gHeight;
constraints.fill = fill;
constraints.weightx = weightX;
constraints.weighty = weightY;
return constraints;
}
}
uj5u.com熱心網友回復:
原答案
Oracle 有一個有用的教程,
不要擴展 Swing 組件或任何其他 Java 類,除非您想覆寫一個或多個類方法。使用組合而不是繼承。
您可以使用多個簡單的方法JPanels來創建復雜的 GUI。該GridBagLayout有它的用途,如創建表單。但所需要的只是多個BorderLayouts.
不要讓 Swing 組件類欄位,除非您需要從多個方法訪問欄位實體。
組織你的代碼,讓它讀起來像一篇文章,最重要的代碼在頂部,更詳細的代碼在底部。理想情況下,您的代碼的讀者將永遠不必翻頁來找出某些東西。不,編譯器不關心方法的順序。但人類讀者肯定會這樣做。
這是完整的可運行代碼。
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class ExampleBorerLayoutGUI {
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
new ExampleBorerLayoutGUI();
});
}
public ExampleBorerLayoutGUI() {
JFrame frame = new JFrame("");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel(), BorderLayout.CENTER);
frame.add(createButtonPanel(), BorderLayout.EAST);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createMainPanel() {
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JTextArea textArea = new JTextArea(5, 40);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
JScrollPane scrollPane = new JScrollPane(textArea);
panel.add(scrollPane, BorderLayout.CENTER);
return panel;
}
private JPanel createButtonPanel() {
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JButton button1 = new JButton("Button 1");
panel.add(button1, BorderLayout.NORTH);
JButton button2 = new JButton("Button 2");
panel.add(button2, BorderLayout.SOUTH);
return panel;
}
}
編輯添加
這是帶有三個JButtons. 你沒有說你想要頂部還是底部的第三個按鈕。我選擇了頂部。

我為上按鈕選擇了GridLayoutJPanel。這很容易添加更多JButtons的GridLayout。
這是修改后的完整可運行代碼。
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class ExampleBorderLayoutGUI {
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
new ExampleBorderLayoutGUI();
});
}
public ExampleBorderLayoutGUI() {
JFrame frame = new JFrame("Example Border Layout");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(createMainPanel(), BorderLayout.CENTER);
frame.add(createButtonPanel(), BorderLayout.EAST);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
private JPanel createMainPanel() {
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JTextArea textArea = new JTextArea(5, 40);
textArea.setLineWrap(true);
textArea.setWrapStyleWord(true);
JScrollPane scrollPane = new JScrollPane(textArea);
panel.add(scrollPane, BorderLayout.CENTER);
return panel;
}
private JPanel createButtonPanel() {
JPanel panel = new JPanel(new BorderLayout());
panel.add(createUpperButtonPanel(), BorderLayout.NORTH);
panel.add(createLowerButtonPanel(), BorderLayout.SOUTH);
return panel;
}
private JPanel createUpperButtonPanel() {
JPanel panel = new JPanel(new GridLayout(0, 1, 5, 5));
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JButton button1 = new JButton("Button 1");
panel.add(button1);
JButton button2 = new JButton("Button 2");
panel.add(button2);
return panel;
}
private JPanel createLowerButtonPanel() {
JPanel panel = new JPanel(new BorderLayout());
panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
JButton button = new JButton("Button 3");
panel.add(button, BorderLayout.SOUTH);
return panel;
}
}
uj5u.com熱心網友回復:
如果您使用 Eclipse IDE 的 Window Builder,您可以自動生成正確的 UI 代碼:https : //www.eclipse.org/windowbuilder/
以這種方式設計應用程式要容易得多。在您的 IDE 上進行設定,讓它為您完成繁重的作業。如果您希望合并 UI 元素的動態調整大小,請將代碼設計為回應式。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/385139.html
上一篇:我在JFrame中看不到任何東西
