Java 如何在west Jpanel(itempanel)下制作一列简单的按钮?
我添加了一个Java 如何在west Jpanel(itempanel)下制作一列简单的按钮?,java,swing,user-interface,layout,Java,Swing,User Interface,Layout,我添加了一个JButton,但它的大小与JPanel相同。添加的任何其他jb按钮都不会显示。 我尝试过许多布局管理器,但他们要么给我错误,要么把按钮放在非常不正确的位置。 任何帮助都将不胜感激 public class FurnitureShop extends JFrame { public static void main(String[] args) { new FurnitureShop().setVisible(true); } private FurnitureShop(
JButton
,但它的大小与JPanel
相同。添加的任何其他jb按钮都不会显示。
我尝试过许多布局管理器,但他们要么给我错误,要么把按钮放在非常不正确的位置。
任何帮助都将不胜感激
public class FurnitureShop extends JFrame {
public static void main(String[] args) {
new FurnitureShop().setVisible(true);
}
private FurnitureShop() {
setSize(800, 600);
setResizable(false);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new BorderLayout());
JPanel contentpanel = new JPanel(new BorderLayout());
contentpanel.setPreferredSize(new Dimension (800, 100));
contentpanel.setBackground(Color.blue);
JPanel footerpanel = new JPanel(new BorderLayout());
footerpanel.setPreferredSize(new Dimension (800, 100));
footerpanel.setBackground(Color.cyan);
JPanel headerpanel = new JPanel(new BorderLayout());
headerpanel.setPreferredSize(new Dimension (400, 200));
headerpanel.setBackground(Color.black);
JPanel itempanel = new JPanel(new BorderLayout());
itempanel.setPreferredSize(new Dimension (400, 200));
itempanel.setBackground(Color.green);
add(contentpanel, BorderLayout.NORTH);
add(footerpanel, BorderLayout.SOUTH);
add(headerpanel, BorderLayout.EAST);
add(itempanel, BorderLayout.WEST);
JButton button = new JButton();
button.setPreferredSize(new Dimension (20, 20));
//button.setLocation(20,20);
button.setVisible(true);
itempanel.add(button);
我的第一个想法是使用GridBagLayout
,虽然这会起作用,但我想知道是否有更简单的方法
这将使用带有边框布局的JPanel
作为主容器(添加到框架的东部
位置)。在NORTH
位置放置一个JPanel
,其中GridLayout
配置为使用单个列
这就是所谓的复合布局。它允许您结合一系列布局管理器的功能来满足您的需要。单个布局管理器能够满足您的所有需求并不常见,并且能够将责任分离是一个非常强大的概念,它允许您专注于单个需求
请查看以了解更多详细信息
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class ButtonColumns {
public static void main(String[] args) {
new ButtonColumns();
}
public ButtonColumns() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
}
try {
JLabel label = new JLabel(new ImageIcon(ImageIO.read(new File("\path\to\images"))));
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(label);
JPanel buttons = new JPanel(new GridLayout(0, 1));
for (int index = 0; index < 10; index++) {
buttons.add(new JButton(String.valueOf(index)));
}
JPanel right = new JPanel(new BorderLayout());
right.add(buttons, BorderLayout.NORTH);
frame.add(right, BorderLayout.EAST);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (IOException exp) {
exp.printStackTrace();
}
}
});
}
}
导入java.awt.BorderLayout;
导入java.awt.EventQueue;
导入java.awt.GridLayout;
导入java.io.File;
导入java.io.IOException;
导入javax.imageio.imageio;
导入javax.swing.ImageIcon;
导入javax.swing.JButton;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JPanel;
导入javax.swing.UIManager;
导入javax.swing.UnsupportedLookAndFeelException;
公共类按钮栏{
公共静态void main(字符串[]args){
新按钮柱();
}
公共按钮栏(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(ClassNotFoundException |实例化Exception | IllegalacessException |不支持ookandfeelException ex){
}
试一试{
JLabel label=newjlabel(新图像图标(ImageIO.read(新文件(“\path\to\images”)));
JFrame=新JFrame(“测试”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(新的BorderLayout());
框架。添加(标签);
JPanel按钮=新的JPanel(新的网格布局(0,1));
对于(int-index=0;index<10;index++){
添加(新的JButton(String.valueOf(index));
}
JPanel right=newjpanel(newborderlayout());
右。添加(按钮,边框布局。北);
框架。添加(右侧,边框布局。东部);
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}捕获(IOEXP异常){
exp.printStackTrace();
}
}
});
}
}
“我尝试过许多布局管理器,但它们要么给我错误信息,要么将按钮放在极不正确的位置。”您尝试过哪些布局管理器?你犯了什么错误?您是否有任何错误放置的屏幕截图?“我尝试了许多布局管理器,但它们要么给我错误信息,要么将按钮放置在极不正确的位置。”
——没有任何东西可以帮助我们了解您的错误。解决方案是使用布局管理器的合理组合,这样您就走上了正确的道路。继续阅读教程,因为如果您了解如何使用它们,您将知道该怎么做。提示:使用JPanel的GridLayout按住按钮,将其放置在使用JPanel的BorderLayout中。同样,请阅读布局管理器教程——它们都在那里!嘘!你总是打我。正如a:P OP在西部总是说+1。@peeskillet Hay,我不会为他们做任何事;)