有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

swing在Java中设置按钮的位置

我正在尝试向我的JPanel添加一个按钮并设置位置。我有

buttonOptions.setLocation(1150, 700);

但它将它添加到我的JPanel中间,大约600,20。我试着加上

buttonOptions.setLocation(1150, 700);

在将按钮添加到面板后,但这也无法修复它。我还设置了动作来设置位置,并且可以工作

public class StartScreen extends JPanel implements ActionListener{
    ImageIcon imageButtonOptions = new ImageIcon(imageButtonOptionsPath);
    ImageIcon imageButtonOptionsHovered = new ImageIcon(imageButtonOptionsHoveredPath);

    JButton buttonOptions = new JButton(imageButtonOptions);

    public StartScreen() {  
        buttonOptions.setBorderPainted(false);  
        buttonOptions.setFocusPainted(false);  
        buttonOptions.setContentAreaFilled(false);
        buttonOptions.addActionListener(this);
        buttonOptions.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                buttonOptions.setIcon(imageButtonOptionsHovered);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                buttonOptions.setIcon(imageButtonOptions);
            }
        });
        buttonOptions.setLocation(1150, 700);
        add(buttonOptions);
    }

共 (2) 个答案

  1. # 1 楼答案

    您当前的问题是,上面的代码不尊重默认使用的布局管理器

    最好的解决方案是使用布局管理器为您进行组件布局,包括嵌套JPanel,每个组件都使用自己的布局。有些人可能建议您使用空布局,在大多数情况下,这样做是错误的,因为它使您的程序很难维护,几乎不可能升级

    顺便问一下,你想把按钮放在哪里?在GUI的右下角

    另外,与其在JButton中使用鼠标侦听器(这通常是个坏主意),不如在JButton的模型中添加一个ChangeListener。然后你可以很容易地看到鼠标是否在按钮上方

    编辑
    你说:

    Yes, the lower right hand corner.

    然后一种方法是使用GridBagLayout并通过在GridBagConstraints参数中使用适当的常量将按钮放置在右下角

    编辑1
    例如:

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class StartScreen extends JPanel implements ActionListener {
       private static final int PREF_W = 1200;
       private static final int PREF_H = 720;
       JButton buttonOptions = new JButton("Options");
    
       @Override
       public Dimension getPreferredSize() {
          return new Dimension(PREF_W, PREF_H);
       }
    
       public StartScreen() {
          setLayout(new GridBagLayout());
          GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0,
                GridBagConstraints.SOUTHEAST, GridBagConstraints.NONE, new Insets(
                      5, 5, 5, 5), 0, 0);
          add(buttonOptions, gbc);
       }
    
       @Override
       public void actionPerformed(ActionEvent e) {
          // TODO Auto-generated method stub
    
       }
    
       private static void createAndShowGui() {
          StartScreen mainPanel = new StartScreen();
    
          JFrame frame = new JFrame("StartScreen");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    

    编辑2
    现在,图标可以在悬停或“翻滚”时更改。我错了,没有必要听ButtonModel的状态。只需设置按钮图标及其滚动图标,按钮即可为您交换:

    import java.awt.*;
    import java.awt.event.*;
    import java.awt.image.BufferedImage;
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class StartScreen extends JPanel {
       private static final int PREF_W = 1200;
       private static final int PREF_H = 720;
       private static final int BI_WIDTH = 100;
       private static final int BI_HEIGHT = 30;
    
       private JButton buttonOptions = new JButton();
       private Icon nonHoveredIcon;
       private Icon hoveredIcon;
    
       @Override
       public Dimension getPreferredSize() {
          return new Dimension(PREF_W, PREF_H);
       }
    
       public StartScreen() {
          hoveredIcon = createIcon("Hovered");
          nonHoveredIcon = createIcon("Non-Hovered");
    
          buttonOptions.setIcon(nonHoveredIcon);
          buttonOptions.setRolloverIcon(hoveredIcon);
    
          setLayout(new GridBagLayout());
          GridBagConstraints gbc = new GridBagConstraints(0, 0, 1, 1, 1.0, 1.0,
                GridBagConstraints.SOUTHEAST, GridBagConstraints.NONE, new Insets(
                      5, 5, 5, 5), 0, 0);
          add(buttonOptions, gbc);
       }
    
       private ImageIcon createIcon(String text) {
          BufferedImage img = new BufferedImage(BI_WIDTH, BI_HEIGHT, 
                BufferedImage.TYPE_INT_ARGB);
          Graphics g = img.getGraphics();
          g.setColor(Color.black);
          Graphics2D g2 = (Graphics2D) g;
          g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, 
                RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
          g.drawString(text, 10, 20);
          g.dispose();
          return new ImageIcon(img);
       }
    
       private static void createAndShowGui() {
          StartScreen mainPanel = new StartScreen();
    
          JFrame frame = new JFrame("StartScreen");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(mainPanel);
          frame.pack();
          frame.setLocationByPlatform(true);
          frame.setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                createAndShowGui();
             }
          });
       }
    }
    
  2. # 2 楼答案

    JPanel默认情况下使用FlowLayout。这将(通常)在水平流中布局组件,默认情况下,从顶部中心开始,一个挨着一个

    问题是,为什么需要绝对定位