【发布时间】:2021-10-27 04:15:12
【问题描述】:
我一直在尝试调整我的 JPanel 大小,同时使用 LayoutManager(例如 BoxLayout)以特定顺序排列该布局内的组件,我通过 NOT USING Box 实现了我希望它的外观布局管理器。
我想要在这张图片中的原始想法:
但我想在为面板使用 Box Layout Manager 时实现这一点,当我这样做时,我会得到以下输出:
我添加 BoxLayout 时得到的输出:
这是代码:
AppPanel.java:
import javax.swing.*;
import java.awt.*;
public class AppPanel extends JPanel {
int Width, Height;
MenuButton mb;
AppPanel(int width, int height)
{
this.Width = width;
this.Height = height;
this.setMinimumSize(new Dimension(this.Width/ 4, this.Height));
this.setPreferredSize(new Dimension(3 * this.Width/ 4, this.Height));
this.setMaximumSize(new Dimension(this.Width/ 4, this.Height));
this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
this.setBackground(Color.GREEN);
this.mb = new MenuButton("HELLO",this.getPreferredSize());
this.add(mb);
}
public void update(int width, int height){
this.Width = width;
this.Height = height;
this.setPreferredSize(new Dimension(3 * this.Width/ 4, this.Height));
// this.mb.update(this.mb.getGraphics());
}
}
MenuPanel.java:
import javax.swing.*;
import java.awt.*;
public class MenuPanel extends JPanel {
JLabel l_AppMenu;
int Width, Height;
MenuButton mb;
MenuPanel(int width, int height)
{
this.Width = width;
this.Height = height;
this.setMinimumSize(new Dimension(this.Width/ 4, this.Height));
this.setPreferredSize(new Dimension(this.Width/ 4, this.Height));
this.setMaximumSize(new Dimension(this.Width/ 4, this.Height));
this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
this.setBackground(Color.BLACK);
this.mb = new MenuButton("HELLO 1",this.getPreferredSize());
l_AppMenu = new JLabel("App Menu");
l_AppMenu.setForeground(Color.GRAY);
this.add(l_AppMenu);
this.add(mb);
}
public void update(int width, int height){
this.Width = width;
this.Height = height;
this.setPreferredSize(new Dimension(3 * this.Width/ 4, this.Height));
// this.mb.update(this.mb.getGraphics());
}
}
MainFrame.java:
import javax.swing.*;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
public class MainFrame extends JFrame implements ComponentListener {
int Width, Height;
MenuPanel mp;
AppPanel ap;
public MainFrame()
{
this.Width = 1600;
this.Height = 900;
mp = new MenuPanel(this.Width, this.Height);
ap = new AppPanel(this.Width, this.Height);
this.setSize(this.Width, this.Height);
this.setVisible(true);
this.setTitle("ToolKit");
this.setDefaultCloseOperation(this.EXIT_ON_CLOSE );
this.add(mp);
this.add(ap);
addComponentListener(this);
this.getContentPane().setLayout(new BoxLayout(this.getContentPane(),BoxLayout.X_AXIS));
}
public static void main(String[] args)
{
MainFrame m = new MainFrame();
}
@Override
public void componentResized(ComponentEvent componentEvent) {
System.out.println(this.getWidth() + " " + this.getHeight());
mp.update(this.getWidth(), this.getHeight());
ap.update(this.getWidth(), this.getHeight());
// this.setSize(this.getHeight() * 16 / 9 , this.getHeight());
}
@Override
public void componentMoved(ComponentEvent componentEvent) {
}
@Override
public void componentShown(ComponentEvent componentEvent) {
}
@Override
public void componentHidden(ComponentEvent componentEvent) {
}
}
MenuButton.java:
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class MenuButton extends JButton implements MouseListener {
String Text;
int Entered = 0;
int Pressed = 0;
MenuButton(String str, Dimension d)
{
super(str);
this.Text = str;
addMouseListener(this);
this.setMinimumSize(new Dimension((int) d.getWidth() - 20,40));
this.setMaximumSize(new Dimension((int) d.getWidth() - 20,40));
this.setPreferredSize(new Dimension((int) d.getWidth() - 20,40));
this.setBorder(null);
}
public void paintComponent(Graphics g)
{
if(this.Pressed == 0) {
g.setColor(new Color(182, 25, 25));
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(new Color(255, 107, 107));
g.fillRect(10, 10, this.getWidth() - 20, this.getHeight() - 20);
}
else{
g.setColor(new Color(255, 107, 107));
g.fillRect(0, 0, this.getWidth(), this.getHeight());
g.setColor(new Color(255, 107, 107));
g.fillRect(10, 10, this.getWidth() - 20, this.getHeight() - 20);
}
if(this.Entered == 0) {
g.setColor(new Color(253, 210, 199));
g.drawString(this.Text, this.getWidth() / 2 - g.getFontMetrics().stringWidth(this.Text) / 2, 25);
}
else{
g.setColor(new Color(1, 36, 67));
g.drawString(this.Text, this.getWidth() / 2 - g.getFontMetrics().stringWidth(this.Text) / 2, 25);
}
}
@Override
public void mouseClicked(MouseEvent mouseEvent) {
}
@Override
public void mousePressed(MouseEvent mouseEvent) {
this.Pressed = 1;
}
@Override
public void mouseReleased(MouseEvent mouseEvent) {
this.Pressed = 0;
}
@Override
public void mouseEntered(MouseEvent mouseEvent) {
this.Entered = 1;
}
@Override
public void mouseExited(MouseEvent mouseEvent) {
this.Entered = 0;
}
}
【问题讨论】:
-
使用布局管理器的重点是避免使用
this.Width = 1600; this.Height = 900;之类的东西。 -
我一直在尝试调整我的 JPanel 尺寸 - 怎么做?对期望的行为进行口头描述。图像有助于显示发生的情况,但无法解释要求。您也不应该使用 ComponentListener。基本上,您的整个代码都在尝试创建自己的布局管理器,并且不允许布局管理器完成其工作。
-
但是布局管理器会根据组件属性对 r8 进行布局,我如何为窗口提供大小会中断布局管理器的工作?????
标签: java swing size layout-manager boxlayout