2013-03-10 138 views
-2

我在嘗試向我的JPanel添加一個按鈕並設置位置。我有在Java中設置按鈕的位置

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); 
    } 
+4

代替的MouseListener你大概可以使用'setRolloverIcon(...)'方法。 – camickr 2013-03-10 05:17:10

回答

3

你目前的問題是你上面的代碼不尊重默認使用的佈局管理器。

最好的解決方案是使用佈局管理器來爲你做組件,包括嵌套JPanels的佈局,每個使用其自己的佈局。有些人可能會建議您使用空佈局,並且在大多數情況下這是錯誤的做法,因爲它會使您的程序非常難以維護並且幾乎不可能升級。

順便說一下,你想在哪裏放置按鈕?在GUI的右下角?

此外,而不是使用與您的JButton,這通常是一個壞主意一個的MouseListener,黃油一個ChangeListener添加到JButton的模型。然後你可以很容易地看到鼠標是否在按鈕上。

編輯
幽州:

是的,右下角。

然後,一種方法是使用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(); 
     } 
     }); 
    } 
} 
+0

是的,在右下角。 – MillerTime 2013-03-10 05:11:23

+0

@MillerTime:請參閱編輯。 – 2013-03-10 05:12:54

+1

我不同意調用null佈局邪惡是有益的。一個簡單的工具比使用佈局管理器更簡單,更有效地實現使用空佈局。佈局管理器是很多java程序員,尤其是初學者討厭java的原因。做不止一種方法,沒有「正確的方式」去做某件事,並說「在大多數情況下做錯事是純粹的主觀的」。在大多數情況下,這是正確的做法,因爲它更容易實現,測試並轉變爲佈局管理器。祝你好運,保持gridbaglayout作爲初學者。 – Dmitry 2013-03-10 05:13:19

5

JPanel,默認情況下,在默認情況下使用FlowLayout。這樣會將組件(通常)排列在水平流中,默認情況下從頂部中心開始一個接一個地排列。

的問題是,爲什麼你需要絕對定位

+0

正好!和1 + – 2013-03-10 05:13:29

+0

我在技術上不需要絕對定位,但它會更好,我忘了面板默認使用了flowlayout,所以我需要將它設置爲null。不幸的是,將佈局設置爲null會導致按鈕不能繪製 – MillerTime 2013-03-10 05:17:52

+3

將佈局管理器設置爲空,不僅要求您定位組件,還要提供大小。絕對定位一般不會鼓勵Swing出來很好的理由(對不起,我哈哈我已經在我的膝蓋上睡了10個月,所以我不能在一個月內減少一個例子) – MadProgrammer 2013-03-10 05:18:58