gpt4 book ai didi

java - 不同尺寸的 Swing 面板

转载 作者:行者123 更新时间:2023-12-03 01:19:07 27 4
gpt4 key购买 nike

我正在尝试将组件放入不同尺寸的面板中。但是,我意识到 GridLayout 将大小相同的部分分开。如何实现如下图所示

enter image description here

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

class PanelDemo {

PanelDemo() {

// Create a new JFrame container. Use the default
// border layout.
JFrame jfrm = new JFrame("Use Three JPanels with Different Sizes");

// Specify FlowLayout manager.
jfrm.getContentPane().setLayout(new GridLayout(1, 3));

// Give the frame an initial size.
jfrm.setSize(900, 300);

// Terminate the program when the user closes the application.
jfrm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Create the first JPanel.
JPanel jpnl = new JPanel();

jpnl.setLayout(new GridLayout(2, 4));

// Set the preferred size of the first panel.
jpnl.setPreferredSize(new Dimension(500, 300));

// Make the panel opaque.
jpnl.setOpaque(true);

// Add a blue border to the panel.
jpnl.setBorder(
BorderFactory.createLineBorder(Color.BLUE));

// Create the second JPanel.
JPanel jpnl2 = new JPanel();

//jpnl2.setLayout(new FlowLayout());
// Set the preferred size of the second panel.
jpnl2.setPreferredSize(new Dimension(300, 300));

// Make the panel opaque.
jpnl2.setOpaque(true);

jpnl2.setBorder(
BorderFactory.createLineBorder(Color.RED));

JPanel jpnl3 = new JPanel();
jpnl3.setOpaque(true);
jpnl3.setPreferredSize(new Dimension(100, 300));
jpnl3.setBorder(
BorderFactory.createLineBorder(Color.ORANGE));

// Add the panels to the frame.
jfrm.getContentPane().add(jpnl);
jfrm.getContentPane().add(jpnl3);
jfrm.getContentPane().add(jpnl2);

// Display the frame.
jfrm.setVisible(true);
}

public static void main(String args[]) {
// Create the frame on the event dispatching thread.
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new PanelDemo();
}
});
}
}

最佳答案

enter image description here

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;

public class TwoPanelWithButtonsLayout {

private JComponent ui = null;
private Insets buttonMargin = new Insets(10,10,10,10);

TwoPanelWithButtonsLayout() {
initUI();
}

public void initUI() {
if (ui!=null) return;

ui = new JPanel(new BorderLayout(4,4));
ui.setBorder(new EmptyBorder(4,4,4,4));

int gap = 5;

JPanel buttons1 = new JPanel(new GridLayout(2, 4, gap, gap));
// 50 is the gap on right, alter as needed
buttons1.setBorder(new EmptyBorder(0, 0, 0, 50));
for (int ii=1; ii<9; ii++) {
buttons1.add(getBigButton("" + ii));
}
ui.add(buttons1, BorderLayout.CENTER);

JPanel buttons2 = new JPanel(new GridLayout(2, 2, gap, gap));
for (int ii=1; ii<5; ii++) {
buttons2.add(getBigButton("" + ii));
}
ui.add(buttons2, BorderLayout.LINE_END);
}

private JButton getBigButton(String text) {
JButton b = new JButton(text);
Font f = b.getFont();
b.setFont(f.deriveFont(f.getSize()*3f));
b.setMargin(buttonMargin);
return b;
}

public JComponent getUI() {
return ui;
}

public static void main(String[] args) {
Runnable r = new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception useDefault) {
}
TwoPanelWithButtonsLayout o = new TwoPanelWithButtonsLayout();

JFrame f = new JFrame(o.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);

f.setContentPane(o.getUI());
f.pack();
f.setMinimumSize(f.getSize());

f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
}
}

关于java - 不同尺寸的 Swing 面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38729936/

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