2013-10-14 51 views
0

我試圖改變我的JFrame中按鈕的大小。我看到這個問題的每一個迴應都說使用button.setPreferredSize(new Dimension(x,y))或button.setSize(new Dimension(x,y)),但這些都不起作用。按鈕標題顯示在正確的位置,但該按鈕佔據整個屏幕。我也嘗試將按鈕添加到框架而不是窗格。當我這樣做時,按鈕是正確的大小,但它是在圖像之外。如何定位和調整此按鈕的大小以使其顯示在圖像上?更改按鈕大小

public ImageTest(String title, String imageSource, int minimum, int middle, int maximum, int curr, boolean x) { // Extends JFrame... 
     frame = new JFrame(); 
     frame.setTitle(title); 
     frame.setLayout(new GridBagLayout()); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     maxim = maximum; 
     minim = minimum; 
     mercSize(minim, maxim, curr); //change size of mercury in thermometer 

     pane = new PaintPane(new ImageIcon(imageSource).getImage()); 
     pane.setLayout(new BorderLayout()); 
     frame.add(pane); 

     b1 = new JButton("Increase Temp"); 
     b1.setPreferredSize(new Dimension(100, 100)); //Button does not work yet 
     b1.setBorder(BorderFactory.createEmptyBorder(560,250,0,0)); 
     b1.setLocation(95, 45); 
     pane.add(b1); 
     frame.pack(); 

     min = new JLabel("" + minimum); 
     min.setFont(min.getFont().deriveFont(Font.BOLD, 28)); 
     if(x == true) min.setForeground(Color.BLACK); //Changes the font color to match the image 
     else min.setForeground(Color.WHITE); 
     min.setVerticalAlignment(JLabel.TOP); 
     min.setVerticalTextPosition(JLabel.TOP); 
     min.setBorder(BorderFactory.createEmptyBorder(445, 150, 0, 0)); //Positions the text 
     pane.add(min); 
     frame.pack(); 

     mid = new JLabel("" + middle); 
     mid.setFont(mid.getFont().deriveFont(Font.BOLD, 28)); 
     if(x == true) mid.setForeground(Color.BLACK); //Changes the font color to match the image 
     else mid.setForeground(Color.WHITE); 
     mid.setVerticalAlignment(JLabel.TOP); 
     mid.setVerticalTextPosition(JLabel.TOP); 
     mid.setBorder(BorderFactory.createEmptyBorder(240, 150, 0, 0)); //Positions the text 
     pane.add(mid); 
     frame.pack(); 

     max = new JLabel("" + maximum); 
     max.setFont(max.getFont().deriveFont(Font.BOLD, 28)); 
     if(x == true) max.setForeground(Color.BLACK); //Changes the font color to match the image 
     else max.setForeground(Color.WHITE); 
     max.setVerticalAlignment(JLabel.TOP); 
     max.setVerticalTextPosition(JLabel.TOP); 
     max.setBorder(BorderFactory.createEmptyBorder(35, 150, 0, 0)); //Positions the text 
     pane.add(max); 
     frame.pack(); 

     temp = new JLabel("Current Temperature: " + curr); 
     temp.setFont(temp.getFont().deriveFont(Font.BOLD, 28)); 
     if(x == true) temp.setForeground(Color.BLACK); //Changes the font color to match the image 
     else temp.setForeground(Color.WHITE); 
     temp.setVerticalAlignment(JLabel.TOP); 
     temp.setVerticalTextPosition(JLabel.TOP); 
     temp.setBorder(BorderFactory.createEmptyBorder(560, 120, 0, 0)); //Positions the text 
     pane.add(temp); 
     frame.pack(); 

     error = new JLabel(); 

     frame.setLocationRelativeTo(null); 
     frame.setVisible(true); 
    } 
+0

的JLabel,JButton的....已(在API實現)水平和垂直對齊 – mKorbel

回答

2

您有一系列複合問題。基本上,您將PaintPane的佈局管理器設置爲BorderLayout,然後嘗試向其添加一組組件。

BorderLayout將根據多種因素(如組件的首選大小和外部組件的最小大小,如果我沒有記錯的話,將如何最好地「認爲」內容應該佈置的決定做出決定。 ..)

讓我們來仔細看看...

public ImageTest(String title, String imageSource, int minimum, int middle, int maximum, int curr, boolean x) { // Extends JFrame... 
    //... 
    pane = new PaintPane(new ImageIcon(imageSource).getImage()); 
    pane.setLayout(new BorderLayout()); 
    // Add button to center position 
    pane.add(b1); 
    // Add min to center position, overwriting b1 
    pane.add(min); 
    // Add mid to center position, overwriting 
    pane.add(mid); 
    // Add max to center position, overwriting mid 
    pane.add(max); 
    // Add temp to center position, overwriting max 
    pane.add(temp); 
    frame.pack(); 
    //... 
} 

每次添加新的組件到PaintPane,你是,有效地去除什麼都在那裏之前(它仍然在容器上它只是不會顯示)。這是因爲BorderLayout將永遠只允許單個組件出現在它的5個佈局位置中的每一個。默認位置始終爲CENTER

這意味着temp是最後添加的組件,幀將使用它的首選大小作爲計算幀大小的基礎(以及可能在幀上可顯示的任何其他組件)

更好的解決辦法可能是使用不同的佈局管理器...

你也應該看看Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?

+0

當我拿走了JButton代碼,其他所有東西都顯示在正確的位置,非常好。每個pane.add()後面的frame.pack()似乎都照顧到了這一點。我會嘗試一個不同的佈局管理器,雖然 – Radi0actvChickn

+0

試着在'frame.setVisible(true)'之前或之後添加一個'pane.revalidate' ... – MadProgrammer

+0

這似乎沒有做任何不同 – Radi0actvChickn