gpt4 book ai didi

java - JFrame GridBagLayout 组件定位

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:46:52 33 4
gpt4 key购买 nike

因此,我使用的是空布局,但被告知这是一个糟糕的主意,因此决定使用 GridBagLayout,因为它被认为是最容易定制的。我在定位我的组件时遇到了问题,我真的很困惑如何在屏幕的顶部中间放置一个标签,在中间放置我的表格,在右下角放置我的登录按钮。有谁知道如何做到这一点?

这是我的代码:

import javafx.geometry.HorizontalDirection;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.*;

public class Main
{

JFrame window = new JFrame("PE Fixture"); //JFrame variables

JPanel container = new JPanel();
JPanel guestFixturesPanel = new JPanel();
JPanel loginPanel = new JPanel();
JPanel adminFixturesPanel = new JPanel();
JPanel createPanel = new JPanel();

String[] columns = {"Sport", "Location", "Date", "Result"};
String[][] data = {{"Football", "AQA Highschool", "12.11.13", "5 - 0"},
{"Tennis", "Wembley", "26.11.14.", "TBC"}};


CardLayout cardLayout = new CardLayout();
GridBagConstraints c = new GridBagConstraints();

public Main()
{

container.setLayout(cardLayout);

guestFixturesPanel.setLayout(new GridBagLayout()); //GUEST FIXTURES PANEL COMPONENTS

JButton loginButton = new JButton("Login");
c.gridx = 0;
c.gridy = 2;
c.insets = new Insets(0, 0, 10, 0);
guestFixturesPanel.add(loginButton, c);
JTable fixturesTable = new JTable(data, columns)
{
public boolean isCellEditable(int data, int columns)
{
return false;
}
};
fixturesTable.setPreferredScrollableViewportSize(new Dimension(500, 300));
fixturesTable.setFillsViewportHeight(true);
JScrollPane scrollTable = new JScrollPane(fixturesTable);
c.gridx = 0;
c.gridy = 0;
c.ipady = 700;
c.ipadx = 400;
guestFixturesPanel.add(scrollTable, c);
JLabel fixturesLabel = new JLabel("FIXTURES");
fixturesLabel.setFont(new Font("TimesRoman", Font.PLAIN, 50));
fixturesLabel.setForeground(Color.WHITE);
c.gridx = 0;
c.gridy = 0;
guestFixturesPanel.add(fixturesLabel, c);

container.add(guestFixturesPanel, "2"); //LABELS EACH COMPONENT WITH A NUMBER
container.add(loginPanel, "3");
container.add(adminFixturesPanel, "4");
container.add(createPanel, "5");

guestFixturesPanel.setBackground(Color.DARK_GRAY); //Colours each panel
loginPanel.setBackground(Color.DARK_GRAY);
adminFixturesPanel.setBackground(Color.DARK_GRAY);
createPanel.setBackground(Color.DARK_GRAY);
cardLayout.show(container, "1");

window.add(container); //Creates the frame of the program.
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(500, 860);
window.setResizable(false);
window.setVisible(true);

}

public static void main(String[] args)
{

SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Main();
}
});

}
}

编辑:

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.*;

public class Main
{
String[] columns = {"Sport", "Location", "Date", "Result"};
String[][] data = {{"Football", "AQA Highschool", "12.11.13", "5 - 0"},
{"Tennis", "Wembley", "26.11.14.", "TBC"}};

private void createAndShowGUI()
{
CardLayout layout = new CardLayout();

JPanel guestCard = new JPanel(layout);
JPanel guestTitle = new JPanel(new FlowLayout(FlowLayout.CENTER)); //GUEST TOP PANEL
JLabel fixturesLabel = new JLabel("FIXTURES");
fixturesLabel.setFont(new Font("TimesRoman", Font.PLAIN, 50));
fixturesLabel.setForeground(Color.WHITE);
guestTitle.add(fixturesLabel);
JTable fixturesTable = new JTable(data, columns) //GUEST MID PANEL
{
public boolean isCellEditable(int data, int columns)
{
return false;
}
};
fixturesTable.setPreferredScrollableViewportSize(new Dimension(350, 450));
fixturesTable.setFillsViewportHeight(true);
JScrollPane scrollTable = new JScrollPane(fixturesTable);
JPanel guestBot = new JPanel(new FlowLayout(FlowLayout.RIGHT)); //GUEST BOT PANEL
JButton loginButtonGuest = new JButton("Login");
guestBot.add(loginButtonGuest);



JPanel loginCard = new JPanel(layout); //LOGIN TOP PANEL
JPanel loginTitle = new JPanel(new FlowLayout(FlowLayout.CENTER));
JLabel loginLabel = new JLabel("LOGIN");
loginLabel.setFont(new Font("TimesRoman", Font.PLAIN, 50));
loginLabel.setForeground(Color.WHITE);
loginTitle.add(loginLabel);

JPanel container = new JPanel(new BorderLayout(8,8)); //ADDS CARDS TO CONTAINER
container.add(guestCard, "2");
container.add(loginCard, "3");

guestCard.add(guestTitle, BorderLayout.NORTH); //ADDS COMPONENTS TO CARDS
guestCard.add(scrollTable, BorderLayout.CENTER);
guestCard.add(guestBot, BorderLayout.SOUTH);
loginCard.add(loginTitle, BorderLayout.NORTH);

container.setBackground(Color.DARK_GRAY); //COLOURS CARDS
guestTitle.setBackground(Color.DARK_GRAY);
scrollTable.setBackground(Color.DARK_GRAY);
guestBot.setBackground(Color.DARK_GRAY);

layout.show(container, "1");

JFrame window = new JFrame("PE Fixtures"); //CREATES WINDOW
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().add(container);
window.setSize(400, 700);
window.setLocationRelativeTo(null);
window.setResizable(false);
window.setVisible(true);

}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Main().createAndShowGUI();
}
});
}
}

错误:

"C:\Program Files\Java\jdk1.7.0_51\bin\java" -Didea.launcher.port=7533 "-Didea.launcher.bin.path=C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 13.0.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.7.0_51\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.7.0_51\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.7.0_51\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.7.0_51\jre\lib\jce.jar;C:\Program Files\Java\jdk1.7.0_51\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.7.0_51\jre\lib\jfxrt.jar;C:\Program Files\Java\jdk1.7.0_51\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.7.0_51\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.7.0_51\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.7.0_51\jre\lib\resources.jar;C:\Program Files\Java\jdk1.7.0_51\jre\lib\rt.jar;C:\Program Files\Java\jdk1.7.0_51\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.7.0_51\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.7.0_51\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.7.0_51\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.7.0_51\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.7.0_51\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.7.0_51\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.7.0_51\jre\lib\ext\zipfs.jar;C:\Users\Harry\Desktop\Computer Science Projects\PE Fixtures v2.0\out\production\PE Fixtures v2.0;C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 13.0.2\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain Main
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: cannot add to layout: unknown constraint: 2
at java.awt.BorderLayout.addLayoutComponent(BorderLayout.java:463)
at java.awt.BorderLayout.addLayoutComponent(BorderLayout.java:424)
at java.awt.Container.addImpl(Container.java:1120)
at java.awt.Container.add(Container.java:966)
at Main.createAndShowGUI(Main.java:48)
at Main.access$000(Main.java:8)
at Main$2.run(Main.java:77)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

最佳答案

I am really confused as to how I can get a label to the top middle of the screen, my table in the middle, and my login button to the bottom right.

如你所见GridBagLayout不是最简单的布局管理器(麻烦太多,功能也不多)。要布置这么少量的组件,我建议您使用 Nested Layout方法:

  • 使用BorderLayout布置“主要”面板组件(标签、表格和按钮)。
  • 使用FlowLayout将标签置于顶部面板的中央,并将按钮放在底部面板的右侧。

例子

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;

public class Demo {

private void createAndShowGUI() {

JLabel label = new JLabel("Title");
label.setFont(label.getFont().deriveFont(Font.BOLD | Font.ITALIC, 18));
JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
topPanel.add(label);

DefaultTableModel model = new DefaultTableModel(new Object[]{"Column # 1", "Column # 2"}, 0);
model.addRow(new Object[]{"Property # 1", "Value # 1"});
model.addRow(new Object[]{"Property # 2", "Value # 2"});
model.addRow(new Object[]{"Property # 3", "Value # 3"});

JTable table = new JTable(model);
JScrollPane scrollPane = new JScrollPane(table);

JButton button = new JButton("Log In");
JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
bottomPanel.add(button);

JPanel content = new JPanel(new BorderLayout(8, 8));
content.add(topPanel, BorderLayout.NORTH);
content.add(scrollPane, BorderLayout.CENTER);
content.add(bottomPanel, BorderLayout.SOUTH);

JFrame frame = new JFrame("Demo");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(content);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}

public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new Demo().createAndShowGUI();
}
});
}
}

截图

enter image description here


此外

对于更复杂的 GUI 设计,您可能想尝试 this answer 中建议的这些第三方布局管理器之一。 :

关于java - JFrame GridBagLayout 组件定位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21947493/

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