gpt4 book ai didi

java - Swing ;我将如何以这种格式放置我的组件?

转载 作者:行者123 更新时间:2023-12-02 06:03:57 25 4
gpt4 key购买 nike

所以,我希望我的项目按照以下格式定位;我对定位确实没有信心,但想多了解一点。这是目前的代码:

package com.bleh.harry;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class Main
{
private JMenuBar menuBar;
private JMenu fileMenu, windowMenu, helpMenu;
private JMenuItem fileNew, fileOpen, fileSave, windowTheme, windowLayout, windowProperties, helpWelcome, helpHelp, helpAbout;
private JTextArea mainTextArea;

public

Main()
{
JPanel mainCard = new JPanel(new BorderLayout(8,8));
JPanel mainTop = new JPanel(new FlowLayout(FlowLayout.CENTER));
menuBar = new JMenuBar();
fileMenu = new JMenu("File");
windowMenu = new JMenu("Window");
helpMenu = new JMenu("Help");
menuBar.add(fileMenu);
menuBar.add(windowMenu);
menuBar.add(helpMenu);

final CardLayout layout = new CardLayout(); //ADDS CARDS TO CONTAINER
final JPanel cards = new JPanel(layout);
cards.add(mainCard, "2");

mainCard.add(mainTop, BorderLayout.NORTH);

JFrame window = new JFrame("Pseudo code text editor");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().add(cards);
window.setSize(1280, 720);
window.setLocationRelativeTo(null);
window.setVisible(true);
}

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

enter image description here

最佳答案

您可以使用BorderLayout...

BorderLayout

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class LayoutTest {

private JMenuBar menuBar;
private JMenu fileMenu, windowMenu, helpMenu;
private JMenuItem fileNew, fileOpen, fileSave, windowTheme, windowLayout, windowProperties, helpWelcome, helpHelp, helpAbout;
private JTextArea mainTextArea;

public LayoutTest() {
JPanel mainCard = new JPanel(new BorderLayout(8, 8));
menuBar = new JMenuBar();
fileMenu = new JMenu("File");
windowMenu = new JMenu("Window");
helpMenu = new JMenu("Help");
menuBar.add(fileMenu);
menuBar.add(windowMenu);
menuBar.add(helpMenu);

final CardLayout layout = new CardLayout();
final JPanel cards = new JPanel(layout);
cards.add(mainCard, "2");

JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.add("One", createPane());
tabbedPane.add("Two", createPane());
tabbedPane.add("Three", createPane());
tabbedPane.add("Four", createPane());

mainTextArea = new JTextArea(20, 40);

mainCard.add(tabbedPane, BorderLayout.WEST);
mainCard.add(new JScrollPane(mainTextArea));

JFrame window = new JFrame("Pseudo code text editor");
window.setJMenuBar(menuBar);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().add(cards);
window.pack();
window.setLocationRelativeTo(null);
window.setVisible(true);
}

protected JPanel createPane() {

return new JPanel() {

@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}

};

}

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

这里的问题是,JTabbedPane 想要的空间量取决于它的内容...

您甚至可以尝试使用 GridBagLayout 这可能会给您更多的控制权...

GridBagLayout

import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

public class LayoutTest {

private JMenuBar menuBar;
private JMenu fileMenu, windowMenu, helpMenu;
private JMenuItem fileNew, fileOpen, fileSave, windowTheme, windowLayout, windowProperties, helpWelcome, helpHelp, helpAbout;
private JTextArea mainTextArea;

public LayoutTest() {
JPanel mainCard = new JPanel(new GridBagLayout());
menuBar = new JMenuBar();
fileMenu = new JMenu("File");
windowMenu = new JMenu("Window");
helpMenu = new JMenu("Help");
menuBar.add(fileMenu);
menuBar.add(windowMenu);
menuBar.add(helpMenu);

final CardLayout layout = new CardLayout(); //ADDS CARDS TO CONTAINER
final JPanel cards = new JPanel(layout);
cards.add(mainCard, "2");

JTabbedPane tabbedPane = new JTabbedPane();
tabbedPane.add("One", createPane());
tabbedPane.add("Two", createPane());
tabbedPane.add("Three", createPane());
tabbedPane.add("Four", createPane());

mainTextArea = new JTextArea(20, 40);

GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 0.25;
gbc.weighty = 1;

mainCard.add(tabbedPane, gbc);
gbc.weightx = 0.75;
mainCard.add(new JScrollPane(mainTextArea), gbc);

JFrame window = new JFrame("Pseudo code text editor");
window.setJMenuBar(menuBar);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.getContentPane().add(cards);
window.pack();
window.setLocationRelativeTo(null);
window.setVisible(true);
}

protected JPanel createPane() {

return new JPanel() {

@Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}

};

}

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

请记住,JMenuBar 属于窗口;)

关于java - Swing ;我将如何以这种格式放置我的组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22443386/

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