Java 使用JFrame和JPanel构建GUI时如何使用布局管理器?
我正在尝试构建一个GUI,其外观如下所示: 其主要思想是在侧面有按钮和一个显示结果的表格 以下是我目前掌握的代码:Java 使用JFrame和JPanel构建GUI时如何使用布局管理器?,java,swing,user-interface,layout,layout-manager,Java,Swing,User Interface,Layout,Layout Manager,我正在尝试构建一个GUI,其外观如下所示: 其主要思想是在侧面有按钮和一个显示结果的表格 以下是我目前掌握的代码: public class GUI { private JButton usdJpyButton = new JButton("USD/JPY"); private JButton usdGbpButton = new JButton("USD/GBP"); public void mainScreen(){ JFrame frame
public class GUI {
private JButton usdJpyButton = new JButton("USD/JPY");
private JButton usdGbpButton = new JButton("USD/GBP");
public void mainScreen(){
JFrame frame = new JFrame("Window");
frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
frame.setSize(900,600);
frame.setResizable(false);
frame.setLocationRelativeTo(null);
GridLayout layout = new GridLayout(0,4);
frame.setLayout(layout);
JPanel sidePanel = new JPanel(new GridBagLayout());
sidePanel.setBackground(Color.black);
GridBagConstraints cons = new GridBagConstraints();
cons.insets = new Insets(50,0,0,0);
cons.gridy = 1;
cons.anchor = GridBagConstraints.NORTH;
sidePanel.add(usdJpyButton);
cons.gridy = 2;
sidePanel.add(usdGbpButton);
frame.add(sidePanel);
frame.setVisible(true);
}
按钮没有对齐,我不确定应该使用哪个布局管理器来获得最佳效果。此外,每个按钮将有一个不同表的操作侦听器,因此我是否需要为每个表创建单独的JPanel
我不确定应该使用哪个布局管理器来获得最佳效果
很少使用单个布局管理器。通常,您将使用不同的布局管理器嵌套每个面板,以实现所需的效果
阅读上Swing教程中的部分,了解每个布局管理器的工作示例
在这种情况下,您可能需要两个面板,一个用于按钮,另一个用于单击按钮时显示的面板
因此,对于按钮,您可以使用BorderLayout
创建一个面板。然后使用GridLayout
创建第二个面板。您可以使用GridLayout
将按钮添加到面板中。然后将此面板添加到边框布局的页面的开始部分。然后将此面板添加到框架内容窗格使用的边框布局
的行开始
然后,为了使面板显示每个表,您将使用卡片布局
。然后为每个货币表创建一个单独的面板,并将其添加到CardLayout
。您可以将此面板添加到内容窗格的BorderLayout
的中心
然后,在按钮的ActionListener
中,根据单击的按钮交换CardLayout
中的面板
同样,我提供的教程链接有BorderLayout、GridLayout和CardLayout的工作示例 我推荐MigLayout
manager
与这位经理合作并不特别困难
package com.zetcode;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import net.miginfocom.swing.MigLayout;
public class InstaMetrixEx extends JFrame implements ActionListener {
public InstaMetrixEx() {
initUI();
}
private void initUI() {
JButton btn1 = new JButton("Client Overview");
JButton btn2 = new JButton("Seo Reports");
JButton btn3 = new JButton("Social media reports");
JButton btn4 = new JButton("Campaigns");
JButton btn5 = new JButton("Webmaster tools");
JButton btn6 = new JButton("Dashboard");
JButton btn7 = new JButton("Tasks");
JComboBox combo1 = new JComboBox();
combo1.addItem("Bulk actions");
JButton btn8 = new JButton("Submit");
JTable table = new JTable(20, 7);
JScrollPane spane = new JScrollPane(table);
createLayout(btn1, combo1, btn8, btn2, btn3, btn4,
btn5, btn6, btn7, spane );
setTitle("InstaMetrixEx");
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private void createLayout(JComponent... arg) {
setLayout(new MigLayout());
add(arg[0], "sgx");
add(arg[1], "gapx 10lp, split 2");
add(arg[2], "wrap");
add(arg[3], "split 6, aligny top, flowy, sgx");
add(arg[4], "sgx");
add(arg[5], "sgx");
add(arg[6], "sgx");
add(arg[7], "sgx");
add(arg[8], "sgx");
add(arg[9], "gapx 10lp, spanx, wrap, push, grow");
pack();
}
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(this, "Button clicked",
"Information", JOptionPane.INFORMATION_MESSAGE);
}
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
InstaMetrixEx ex = new InstaMetrixEx();
ex.setVisible(true);
});
}
}
截图:
使用您在构建JPanel
egsidePanel.add(usdJpyButton,cons)时指定的GridBagConstraints代码>当我使用BorderLayout时,屏幕上不能有两个大小不同的面板,当我使用GridLayout时,面板不会覆盖整个屏幕window@Emir54,当然可以。你看过BorderLayout教程吗???如果你这样做了,你可以从图像中看出组件可以是不同的大小!!!我之前已经检查过这些教程,但无法使其正常工作,因此我在stackoverflow上创建了这个问题?@Emir54,请发布您的演示问题。您已经得到了一个解决方案,因此请向我们展示您根据给出的建议尝试编写的代码。若你们不能理解这个工作示例,那个么就发布一个具体的问题。我们猜不出你对什么感到困惑。