Java 在添加由MouseeEvent触发的边框后,JButtons会在gridBagLayout中不断调整大小
我把一个JPanel分成3个单元。 Flure位于16个粒子中每个单元的左侧面板上。 Flyr位于4×4网格中每个单元格的右面板上 无论何时设置边框,我都很难让我的按钮(Flure和Flyr)不调整大小。只要按下按钮,就会设置边框。我认为这与我如何使用gridBagConstraints有关,也可能与我没有设置的大小有关 这是程序启动时的样子: 这是单击Flure和Flyr时的外观:Java 在添加由MouseeEvent触发的边框后,JButtons会在gridBagLayout中不断调整大小,java,swing,mouseevent,jbutton,gridbaglayout,Java,Swing,Mouseevent,Jbutton,Gridbaglayout,我把一个JPanel分成3个单元。 Flure位于16个粒子中每个单元的左侧面板上。 Flyr位于4×4网格中每个单元格的右面板上 无论何时设置边框,我都很难让我的按钮(Flure和Flyr)不调整大小。只要按下按钮,就会设置边框。我认为这与我如何使用gridBagConstraints有关,也可能与我没有设置的大小有关 这是程序启动时的样子: 这是单击Flure和Flyr时的外观: 设置边框会改变按钮的大小一种替代方法,可能是将每个JButton放置在JPanel上,使用GridBag
设置边框会改变按钮的大小一种替代方法,可能是将每个
JButton
放置在JPanel
上,使用GridBagLayout
和所需大小的插图将按钮放置在面板上,并简单地更改JPanel
的背景,处理按钮时,ActionListener
比MosueListener
更合适一个问题是,当JPanel的边框被激活时,它被JPanel内的按钮覆盖。如何为我的JPanel添加内边距,以便按钮不会阻塞边框?正如我所说的,使用GridBagConstraints的insets属性创建一个假边框
import javax.swing.*;
import java.awt.*;
public class GridBagTest {
private static JFrame myFrame;
private static JPanel mainPanel;
public static void main(String[] args) {
createFrame();
createPanels();
constructCells();
myFrame.pack();
myFrame.setVisible(true);
}
private static void createFrame() {
myFrame = new JFrame();
myFrame.setPreferredSize(new Dimension(800, 800));
myFrame.setExtendedState(JFrame.MAXIMIZED_BOTH);
myFrame.setTitle("Simulator");
myFrame.setResizable(true);
}
private static void createPanels() {
mainPanel = new JPanel(new BorderLayout(10, 10));
myFrame.getContentPane().add(mainPanel);
}
private static void constructCells() {
for (int i = 0; i < 3; i++) {
mainPanel.add(new Cell(i + 1), choosePosition(i + 1));
}
}
private static String choosePosition(int number) {
switch (number) {
case 1:
return BorderLayout.EAST;
case 2:
return BorderLayout.CENTER;
case 3:
return BorderLayout.WEST;
}
return null;
}
}
import javax.swing.*;
import java.awt.*;
class Cell extends JButton {
private static final int NUMPARTICLES = 16;
private JPanel leftPanel;
private JPanel rightPanel;
public Cell(int number) {
setLayout(new GridBagLayout());
initFields();
initParticles();
initPanels();
if (number == 1)
this.setBackground(Color.red);
if (number == 2)
this.setBackground(Color.blue);
if (number == 3)
this.setBackground(Color.green);
}
private void initFields(){
this.leftPanel = new JPanel(new GridBagLayout());
this.rightPanel = new JPanel(new GridBagLayout());
}
private void initParticles(){
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
for(int i=0;i<NUMPARTICLES;i++){
gbc.gridy = i;
leftPanel.add(new Particle(),gbc);
}
}
private void initPanels(){
initButtons();
splitPane();
}
private void initButtons(){
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.BOTH;
gbc.ipadx = 10;
gbc.ipady = 30;
for(int i=0;i<4;i++){
gbc.gridx = i+2;
for(int j=0;j<NUMPARTICLES/4;j++){
gbc.gridy = j+2;
rightPanel.add(new Flure(),gbc);
}
}
}
private void splitPane(){
JSplitPane sp = new
JSplitPane(JSplitPane.HORIZONTAL_SPLIT,leftPanel,rightPanel);
sp.setEnabled(false);
sp.setResizeWeight(0.5);
this.add(sp);
}
}
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
public class Particle extends JButton {
public Particle(){
this.setEnabled(false);
this.setBackground(Color.gray);
this.setForeground(Color.white);
this.setLayout(new GridBagLayout());
initButtons();
}
private void initButtons(){
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
this.add(new Flyr(),gbc);
gbc.gridx = 1;
this.add(new Flyr(),gbc);
gbc.gridx = 2;
JButton flurSign = new JButton();
flurSign.setEnabled(false);
flurSign.setBackground(Color.black);
flurSign.setBackground(Color.blue);
this.add(flurSign,gbc);
}
}
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class Flyr extends JButton implements MouseListener {
private static final LineBorder LB = new LineBorder(Color.magenta, 3);
public Flyr(){
setMargin(new Insets(15,15,15,15));
setBackground(Color.black);
setForeground(Color.white);
addMouseListener(this);
}
public void mouseClicked(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mousePressed(MouseEvent e){
setBorder(LB);
}
}
import javax.swing.*;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
public class Flure extends JButton implements MouseListener {
private static final LineBorder LB = new LineBorder(Color.green, 3);
public Flure() {
setBackground(Color.black);
setForeground(Color.white);
setText("UP");
addMouseListener(this);
}
public void mouseClicked(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
setBorder(LB);
}
}