所以我嵌套了多个布局,这些布局是BorderLayout内部的FlowLayout,它位于Card布局中。我有这么多布局的原因是因为我使用FlowLayout来对齐面板中的组件。然后使用BorderLayout将此面板添加到我的主框架中,并使用.NORTH
等对齐此对齐。然后我将这个面板(包含BorderLayout内的FlowLayout的面板)添加到卡片中,这将允许我在面板之间切换。
当我将面板添加到卡片中时,我无法运行程序并收到此错误:
"C:\Program Files\Java\jdk1.7.0_51\bin\java" -Didea.launcher.port=7534 "-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)
CODE:
import javafx.geometry.HorizontalDirection;
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();
}
});
}
}
答案 0 :(得分:1)
我现在修好了。我只是将边框布局放在卡片内,反之亦然。