我一直試圖調整我的JPNEL的大小,同時使用LayoutManager,如BoxLayout,以特定的順序安排該Layout內的組件,我通過不使用Box Layout Manager實作了我想要的樣子。
但我想在為面板使用框式布局管理器時實作這一點,而當我這樣做時,我得到了這樣的輸出:
當我添加 BoxLayout 時,我得到的輸出是:這是代碼 :
AppPanel.java :
import javax.swing.*;
import java.awt.*;
public class AppPanel extends JPanel{
int Width, Height;
選單按鈕 mb;
AppPanel(int width, int height)
{
this.Width = width;
this.Height=高度。
this.setMinimumSize(new Dimension(this. 寬度/ 4, this.Height))。)
this.setPreferredSize(new Dimension(3 * this. 寬度/ 4, this.Height))。)
this.setMaximumSize(new Dimension(this. 寬度/ 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=高度。
this.setPreferredSize(new Dimension(3 * this. 寬度/ 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;
選單按鈕 mb;
MenuPanel(int width, int height)
{
this.Width = width;
this.Height=高度。
this.setMinimumSize(new Dimension(this. 寬度/ 4, this.Height))。)
this.setPreferredSize(new Dimension(this. 寬度/ 4, this.Height))。)
this.setMaximumSize(new Dimension(this. 寬度/ 4, this.Height))。)
this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS)) 。
this.setBackground(Color.BLACK)。
this. mb = new MenuButton("HELLO 1"/span>,this.getPreferredSize()
l_AppMenu = new JLabel("App Menu"/span>)。
l_AppMenu.setForeground(Color.GRAY)。
this.add(l_AppMenu)。
this.add(mb);
}
public void update(int width, int height){
this.Width = width;
this.Height=高度。
this.setPreferredSize(new Dimension(3 * this. 寬度/ 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"/span>)。
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{
字串文本。
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;
}
}
uj5u.com熱心網友回復:
通過閱讀MenuButton的源代碼,我意識到它是根據滑鼠事件來繪制邊框和背景的。由于JButtons同時支持Borders和背景顏色,所以您不必自己做這個。所以您可以創建一個MouseListener來更新這些屬性,并讓它用這些屬性來重繪自己(我將在下面的代碼示例中演示)。
不管怎樣,回到你的主要問題上,你可以通過使用BoxLayouts來做到這一點:
import java.awt.Color;
import java.awt.Component。
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Objects;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Jpanel;
import javax.swing.SwingUtilities;
import javax.swing.border.Border。
public class BoxLayoutTest {
public static final class MainButtonAdapter extends MouseAdapter {
private final Color fgOnEntered, fgOnNotEntered, bgOnPressed, bgOnNotPressed;
private final Border onPressed, onNotPressed;
public MainButtonAdapter(final Color fgOnEntered,
final Color fgOnNotEntered,
final Color bgOnPressed,
final Color bgOnNotPressed,
finalBorder onPressed。
final Border onNotPressed) {
this.fgOnEntered = Objects.requireNonNull(fgOnEntered)。
this.fgOnNotEntered = Objects.requireNonNull(fgOnNotEntered);
this.bgOnPressed = Objects.requireNonNull(bgOnPressed)。
this.bgOnNotPressed = Objects.requireNonNull(bgOnNotPressed);
this.onPressed = Objects.requireNonNull(onPressed);
this.onNotPressed = Objects.requestNonNull(onNotPressed);
}
private void inside(final Component src) {
if (src != null) {
src.setForeground(fgOnEntered);
src.repaint()。
}
}
private void released(final Component src) {
if (src != null) {
if (src instanceof JComponent)
((JComponent) src).setBorder(onNotPressed)。
src.setBackground(bgOnNotPressed)。
src.repaint()。
}
}
//@Override
//public void mouseWheelMoved(final MouseWheelEvent e) {
//inside(e.getComponent());
//}
//
//@Override
//public void mouseDragged(final MouseEvent e) {
// inside(e.getComponent());
//}
@Override
public void mouseMoved(final MouseEvent e){
inside(e.getComponent())。
}
@Override
public void mouseClicked(final MouseEvent e) {
released(e.getComponent())。
}
@Override
public void mousePressed(final MouseEvent e) {
final Component src = e.getComponent()。
if (src != null) {
if (src instanceof JComponent)
((JComponent) src).setBorder(onPressed)。
src.setBackground(bgOnPressed);
src.repaint()。
}
}
@Override
public void mouseReleased(final MouseEvent e) {
released(e.getComponent())。
}
@Override
public void mouseEntered(final MouseEvent e) {
inside(e.getComponent())。
}
@Override
public void mouseExited(final MouseEvent e){
final Component src = e.getComponent()。
if (src != null) {
src.setForeground(fgOnNotEntered)。
src.repaint()。
}
}
}
public static JButton createMainButton(final String text,
final MouseAdapter配接器。
final 邊界border。
final 顏色背景。
final Color foreground,
final ActionListener aL) {
final JButton button = (text == null)? new JButton()。new JButton(text);
if (adapter != null) {
button.addMouseListener(adapter);
button.addMouseMotionListener(adapter);
button.addMouseWheelListener(adapter);
}
if (border != null)
button.setBorder(border)。
if (background != null) {
button.setContentAreaFilled(false)。
button.setOpaque(true)。
button.setBackground(background);
}
if (foreground != null)
button.setForeground(foreground)。
if (aL != null)
button.addActionListener(aL)。
button.setFocusPainted(false)。
return button。
}
public static JPanel createBoxLayoutPanel(final int axis。
final Component... components) {
final JPanel panel = new JPanel()。
panel.setLayout(new BoxLayout(panel, axis))。
if ( components != null)
for (final Component c: components) {
if (!c.isMinimumSizeSet())
c.setMinimumSize(c.getPreferredSize() )。
if (!c.isMaximumSizeSet())
c.setMaximumSize(new Dimension(Short.MAX_VALUE, Short.MAX_VALUE)) 。
panel.add(c)。
}
return panel;
}
public static JLabel label(final String text,
final 組件 labelFor,
final Color foreground) {
final JLabel jLabel = new JLabel(text, JLabel. CENTER)。)
jLabel.setForeground(foreground)。
jLabel.setLabelFor(labelFor)。
return jLabel。
}
public static Box.Filler createNonOpaqueVerticalGlue() {
final Box. Filler vglue = new Box. Filler(new Dimension(0, 0), new Dimension(0, 0), new Dimension(0, Short. MAX_VALUE))。)
vglue.setOpaque(false)。
return vglue。
}
public static void main(final String[] args) {
SwingUtilities.invokeLater(() -> {
final int buttonBorderThickness = 10,
paddingThickness = 20;
final Color borderOnPressed = new Color(182, 25, 25)。)
borderOnNotPressed = new Color(255, 107, 107)。
backgroundOnPressed = new Color(255, 107, 107),
backgroundOnNotPressed = new Color(255, 107, 107)。
foregroundOnEntered = new Color(253, 210, 199)。
foregroundOnNotEntered = new Color(1, 36, 67),
leftBackground = Color.BLACK,
rightBackground = Color.GREEN,
labelForeground = Color.LIGHT_GRAY。
final String firstButtonText = 以長文本為標簽的主按鈕。
secondButtonText = "短"。
labelText = "一些隨機文本。"。
final Border onPressed = BorderFactory.createLineBorder(borderOnPressed, buttonBorderThickness)。
onNotPressed = BorderFactory.createLineBorder(borderOnNotPressed, buttonBorderThickness),
padding = BorderFactory.createEmptyBorder(paddingThickness, paddingThickness, paddingThickness, paddingThickness)。
final MainButtonAdapter mainButtonAdapter = new MainButtonAdapter(foregroundOnEntered, foregroundOnNotEntered, backgroundOnPressed, backgroundOnNotPressed, onPressed, onNotPressed)。)
final JButton firstButton = createMainButton(firstButtonText, mainButtonAdapter, onNotPressed, backgroundOnNotPressed, foregroundOnNotEntered, null)。)
secondButton = createMainButton(secondButtonText, mainButtonAdapter, onNotPressed, backgroundOnNotPressed, foregroundOnNotEntered, null)。
final Dimension secondButtonPreferredSize = secondButton.getPreferredSize();
secondButton.setMaximumSize(new Dimension(Short.MAX_VALUE, secondButtonPreferredSize.height)) 。
final JLabel lbl = label( labelText, firstButton, labelForeground)。
lbl.setMaximumSize(lbl.getPreferredSize() )。
final JPanel firstPanel = createBoxLayoutPanel(BoxLayout. X_AXIS, lbl, Box.createHorizontalStrut(paddingThickness), firstButton)。)
firstPanel.setOpaque(false)。
firstPanel.setMaximumSize(new Dimension(Short.MAX_VALUE, firstPanel.getPreferredSize().height))。
//Ucomment the following lines to get bigger size proportion for the right (green) panel...
//secondButtonPreferredSize.width = Math.max(500, secondButtonPreferredSize.width);
//secondButton.setMinimumSize(secondButtonPreferredSize);
//secondButton.setPreferredSize(secondButtonPreferredSize);
final JPanel left = createBoxLayoutPanel(BoxLayout。 Y_AXIS, firstPanel, createNonOpaqueVerticalGlue() /*replace this "glue" with your components...*/)。)
left.setBorder(padding);
left.setBackground(leftBackground)。
final JPanel right = createBoxLayoutPanel(BoxLayout. Y_AXIS, secondButton, createNonOpaqueVerticalGlue() /*replace this "glue" with your components...*/)。)
right.setBorder(padding);
right.setOpaque(false); //Could almost be 'right.setBackground(rightBackground);' instead.
final JPanel contents = createBoxLayoutPanel(BoxLayout.X_AXIS, left, right)。
contents.setBackground(rightBackground); //這就是為什么我們之前叫'right.setOpaque(false);'。
final JFrame frame = new JFrame("BoxLayout test") 。
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)。
frame.getContentPane().add(contents);
frame.pack();
//frame.setSize(Math.max(1600, frame.getWidth()), Math.max(900, frame.getHeight()) );
frame.setLocationRelativeTo(null)。
frame.setVisible(true)。
});
}
基本上,內容窗格在BoxLayout上包含了兩個彩色面板,X_AXIS。然后左邊的面板包含另一個BoxLayout面板,其中有標簽和第一個按鈕。然后就是照顧到每個Component的最小、首選和最大尺寸。
如果您想避免嵌套面板,我想您可以在一個自定義的油漆面板上使用GridBagLayout(來油漆不同的背景顏色),這也將給您更多的權力來使用權重系數來微調調整行為。不過我還沒有試過。
當你運行它時,記得用滑鼠調整框架的大小,看看它的表現如何。
以后你可以用你想要的Component替換兩個createNonOpaqueVerticalGlue方法呼叫,因為我假設你以后會在黑色和綠色面板內需要更多內容。如果你隨后需要一些垂直間隙,請使用垂直支桿。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/310281.html
標籤:


