可以在Java中使用JPanels,如在<div>
中使用HTML吗?在JFrame中为CardLayout以外的其他东西设置多个JPanel是否与良好做法一致? (例如,如果我的JPanel包含一个位于JFrame内容面板北边界的帮助/约按钮,我使用包含该按钮的按钮放置一些间距以强制按钮为一定的规模...)
具体案例:
假设我正在尝试这样做:,在开始时,我有一个这样的模拟(感谢我在其他地方找到的按钮渲染的一些代码):
。在这种情况下,使用多个JFrame会是一个好例子吗(就像那些HTML
<div>
那样)?如果不是,什么时候会?
答案 0 :(得分:1)
根据您的要求,您不必“使用”多个面板,您可以从单个面板和GridBagLayout
完成您想要的任务,例如......
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
JButton helpButton = new JButton("?");
JTextField projectDirectory = new JTextField(20);
JTextField documentDirectory = new JTextField(20);
JButton projectButton = new JButton("Project");
JButton documentButton = new JButton("Document");
JButton continueButton = new JButton("Continue to files");
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 1;
add(new JLabel("Project Directory"), gbc);
gbc.gridy++;
add(new JLabel("Documentation Directory"), gbc);
gbc.gridx = 1;
gbc.gridy = 1;
add(projectDirectory, gbc);
gbc.gridy++;
add(documentDirectory, gbc);
gbc.gridx = 2;
gbc.gridy = 0;
add(helpButton, gbc);
gbc.gridy++;
add(projectButton, gbc);
gbc.gridy++;
add(documentButton, gbc);
gbc.gridx = 0;
gbc.gridy++;
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(continueButton, gbc);
}
}
}
我将使用多个面板的主要原因是:
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
JButton helpButton = new JButton("?");
JButton continueButton = new JButton("Continue to files");
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.EAST;
add(helpButton, gbc);
gbc.gridy++;
add(new FolderSelectionPane("Project Folder"), gbc);
gbc.gridy++;
add(new FolderSelectionPane("Documentation Folder"), gbc);
gbc.anchor = GridBagConstraints.CENTER;
gbc.gridy++;
add(continueButton, gbc);
}
}
public class FolderSelectionPane extends JPanel {
public FolderSelectionPane(String label) {
JTextField projectDirectory = new JTextField(20);
JButton projectButton = new JButton("Folder");
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.insets = new Insets(0, 2, 0, 2);
add(new JLabel(label), gbc);
gbc.gridx++;
add(projectDirectory, gbc);
gbc.gridx++;
add(projectButton, gbc);
}
}
}