JPanel 不允许定位组件
JPanel doesnt allow positioning component
所以我创建了 2 JFrames
但后来我意识到 2 JFrames
不是一个好的 idea.So 我添加了 JTabbedPane
然后尝试以 'JPanel
的形式添加不同的选项卡。但是我无法像在 Jframe
中那样定位组件,它看起来像这样。
但我希望它是这样的
我该怎么做?
这是我最近用的。只需使用您想要的布局(MigLayout 是我在这里的选择)。
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
JPanel pnlOne = new JPanel();
tabbedPane.addTab("Tab One Name", null, pnlOne, null);
pnlOne.setLayout(new MigLayout("", "[644.00,grow][50px:n,grow][]","[grow][]"));
JPanel pnlTwo = new JPanel();
tabbedPane.addTab("Tab Two Name", null, pnlTwo, null);
pnlTwo.setLayout(new MigLayout("", "[grow][]", "[grow][]"));
没有工作示例,您似乎已经保留了 JPanel
(FlowLayout
)的默认布局。
首先为您的每个视图创建一个自定义面板,这样可以更轻松地管理并将它们应用到 JTabbedPane
。
对于您的直接布局问题,我推荐 GridBagLayout
,因为它的功能和灵活性...
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
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);
JTabbedPane tp = new JTabbedPane();
tp.add("Person", new JPanel());
tp.add("Work Detail", new TestPane());
frame.add(tp);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JTextField name;
private JTextField serial;
private JTextField id;
private JTextField email;
private JComboBox level;
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.EAST;
gbc.insets = new Insets(2, 2, 2, 2);
add(new JLabel("Name"), gbc);
gbc.gridx += 2;
add(new JLabel("Sr No"), gbc);
gbc.gridx = 0;
gbc.gridy++;
add(new JLabel("ID"), gbc);
gbc.gridx += 2;
add(new JLabel("Email ID"), gbc);
name = new JTextField(10);
serial = new JTextField(10);
id = new JTextField(10);
email = new JTextField(10);
level = new JComboBox(new String[]{"Manager", "Eng Lead", "Eng Designer", "Jr Designer"});
gbc.anchor = GridBagConstraints.WEST;
gbc.gridx = 1;
gbc.gridy = 0;
add(name, gbc);
gbc.gridx += 2;
add(serial, gbc);
gbc.gridx = 1;
gbc.gridy++;
add(id, gbc);
gbc.gridx += 2;
add(email, gbc);
gbc.gridx = 0;
gbc.gridy++;
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(level, gbc);
gbc.gridy++;
gbc.anchor = GridBagConstraints.CENTER;
add(new JButton("Save"), gbc);
}
}
}
所以我创建了 2 JFrames
但后来我意识到 2 JFrames
不是一个好的 idea.So 我添加了 JTabbedPane
然后尝试以 'JPanel
的形式添加不同的选项卡。但是我无法像在 Jframe
中那样定位组件,它看起来像这样。
但我希望它是这样的
我该怎么做?
这是我最近用的。只需使用您想要的布局(MigLayout 是我在这里的选择)。
JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
JPanel pnlOne = new JPanel();
tabbedPane.addTab("Tab One Name", null, pnlOne, null);
pnlOne.setLayout(new MigLayout("", "[644.00,grow][50px:n,grow][]","[grow][]"));
JPanel pnlTwo = new JPanel();
tabbedPane.addTab("Tab Two Name", null, pnlTwo, null);
pnlTwo.setLayout(new MigLayout("", "[grow][]", "[grow][]"));
没有工作示例,您似乎已经保留了 JPanel
(FlowLayout
)的默认布局。
首先为您的每个视图创建一个自定义面板,这样可以更轻松地管理并将它们应用到 JTabbedPane
。
对于您的直接布局问题,我推荐 GridBagLayout
,因为它的功能和灵活性...
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
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);
JTabbedPane tp = new JTabbedPane();
tp.add("Person", new JPanel());
tp.add("Work Detail", new TestPane());
frame.add(tp);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private JTextField name;
private JTextField serial;
private JTextField id;
private JTextField email;
private JComboBox level;
public TestPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.EAST;
gbc.insets = new Insets(2, 2, 2, 2);
add(new JLabel("Name"), gbc);
gbc.gridx += 2;
add(new JLabel("Sr No"), gbc);
gbc.gridx = 0;
gbc.gridy++;
add(new JLabel("ID"), gbc);
gbc.gridx += 2;
add(new JLabel("Email ID"), gbc);
name = new JTextField(10);
serial = new JTextField(10);
id = new JTextField(10);
email = new JTextField(10);
level = new JComboBox(new String[]{"Manager", "Eng Lead", "Eng Designer", "Jr Designer"});
gbc.anchor = GridBagConstraints.WEST;
gbc.gridx = 1;
gbc.gridy = 0;
add(name, gbc);
gbc.gridx += 2;
add(serial, gbc);
gbc.gridx = 1;
gbc.gridy++;
add(id, gbc);
gbc.gridx += 2;
add(email, gbc);
gbc.gridx = 0;
gbc.gridy++;
gbc.gridwidth = GridBagConstraints.REMAINDER;
add(level, gbc);
gbc.gridy++;
gbc.anchor = GridBagConstraints.CENTER;
add(new JButton("Save"), gbc);
}
}
}