2017-03-03 51 views
-1

我希望按鈕和文本框在標有紅色的空白處與它們所在的佈局相同。查看圖片以瞭解我的意思。如何將按鈕移動到精確位置?

更新:按鈕都在現在的地方,但圖像不會出現在第二面板上

enter image description here

我怎樣才能將它們移動到那裏?繼承人我的代碼到目前爲止

package gasindicator; 

import java.awt.*; 
import java.awt.event.*; 
import java.awt.image.BufferedImage; 
import javax.swing.*; 
import javax.swing.border.*; 
import javax.swing.plaf.basic.*; 
import java.io.*; 
import javax.imageio.*; 
import java.net.*; 

public class GasIndicator extends JPanel 
{ 
private Image image; 


GasIndicator() 
{ 
    try 
    { 
     image = ImageIO.read(new URL("http://i68.tinypic.com/2ceja8i.png")); 

    } 
    catch (IOException ioe) 
    { 
     System.out.println("Unable to fetch image."); 
     ioe.printStackTrace(); 
    } 

    setLayout(new BorderLayout()); 

    JLabel background = new JLabel(new ImageIcon(image)); 
    background.setLayout(new FlowLayout(FlowLayout.LEFT)); 
    add(background); 

    JPanel buttonPanel = new JPanel(new GridLayout(0, 3, 6, 5)); 
    buttonPanel.setBorder(new EmptyBorder(338, 233, 0, 0)); 

    buttonPanel.setOpaque(false); 


    //for (int i = 0; i < 7; i++) 
    { 
     JButton button = new JButton("Button"); 
     JButton button1 = new JButton("Button"); 
     JButton button2 = new JButton("Button"); 
     JButton button3 = new JButton("Button"); 
     JButton button4 = new JButton("Button"); 
     JButton button5 = new JButton("Button"); 

     button.setPreferredSize(new Dimension(160, 45)); 
     buttonPanel.add(button); 
     buttonPanel.add(button1); 
     buttonPanel.add(button2); 
     buttonPanel.add(button3); 
     buttonPanel.add(button4); 
     buttonPanel.add(button5); 

     button.addActionListener(new Action()); 
    } 

    background.add(buttonPanel); 
} 

static class Action implements ActionListener { 

     public void actionPerformed(ActionEvent e) { 
      JFrame frame2 = new JFrame("Museums in London"); 
      frame2.setVisible(true); 
      frame2.setSize(550, 650); 
      JPanel panel = new JPanel(); 
      frame2.add(panel); 
      Custom contentPane; 

      // JFrame frame = new JFrame("JTextField"); 
      contentPane = new Custom(); 
      frame2.setContentPane(contentPane); 

     } 
    } 

private static void ShowGUI() 
{ 
    JFrame frame = new JFrame("SSCCE"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    frame.add(new GasIndicator()); 
    frame.pack(); 
    frame.setLocationByPlatform(true); 
    frame.setVisible(true); 
} 

public static void main(String[] args) 
{ 
    EventQueue.invokeLater(() -> ShowGUI()); 
/* 
    EventQueue.invokeLater(new Runnable() 
    { 
     public void run() 
     { 
      createAndShowGUI(); 
     } 
    }); 
*/ 
} 

class Custom extends JPanel { 

public BufferedImage image; 

public Custom() { 
    try { 

     image = ImageIO.read(new URL 
("http://www.destination360.com/europe/uk/images/s/museums.jpg")); 
    } catch (IOException ioe) { 
     System.out.println("Unable to fetch image."); 
     ioe.printStackTrace(); 
    } 
} 

public Dimension getPreferredSize() { 
    return (new Dimension(image.getWidth(), image.getHeight())); 
} 

public void paintComponent(Graphics x) { 
    super.paintComponent(x); 
    x.drawImage(image, 10, 10, this); 
} 
    } 
} 
+1

panel.setLayout(null); – ControlAltDel

+3

這裏的其他人會告訴你將佈局設置爲null是不好的設計,你應該學會使用不同的佈局管理器來做到這一點。 – ControlAltDel

+0

我在哪裏放?以及如何將所有按鈕移動到所需位置? –

回答

4

簡單的例子來說明使用佈局管理器帶有邊框的概念。按鈕的大小也作了調整,將圖像中的按鈕的大小:

import java.awt.*; 
import java.awt.event.*; 
import javax.swing.*; 
import javax.swing.border.*; 
import javax.swing.plaf.basic.*; 
import java.io.*; 
import javax.imageio.*; 
import java.net.*; 

public class SSCCE extends JPanel 
{ 
    private Image image; 

    SSCCE() 
    { 
     try 
     { 
      image = ImageIO.read(new URL("http://i68.tinypic.com/2ceja8i.png")); 

     } 
     catch (IOException ioe) 
     { 
      System.out.println("Unable to fetch image."); 
      ioe.printStackTrace(); 
     } 

     setLayout(new BorderLayout()); 

     JLabel background = new JLabel(new ImageIcon(image)); 
     background.setLayout(new FlowLayout(FlowLayout.LEFT)); 
     add(background); 

     JPanel buttonPanel = new JPanel(new GridLayout(0, 3, 6, 5)); 
     buttonPanel.setBorder(new EmptyBorder(338, 233, 0, 0)); 

     buttonPanel.setOpaque(false); 

     for (int i = 0; i < 6; i++) 
     { 
      JButton button = new JButton("Button " + i); 
      button.setPreferredSize(new Dimension(160, 45)); 
      buttonPanel.add(button); 
     } 

     background.add(buttonPanel); 
    } 

    private static void createAndShowGUI() 
    { 
     JFrame frame = new JFrame("SSCCE"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     frame.add(new SSCCE()); 
     frame.pack(); 
     frame.setLocationByPlatform(true); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) 
    { 
     EventQueue.invokeLater(() -> createAndShowGUI()); 
/* 
     EventQueue.invokeLater(new Runnable() 
     { 
      public void run() 
      { 
       createAndShowGUI(); 
      } 
     }); 
*/ 
    } 
} 

是,仍然有值的一些調整。但使用一個EmptyBorder調整整個面板的位置並且同時移動所有按鈕比單獨調整每個按鈕的位置更容易。

注意:不要使用JLabel來顯示圖像,因爲如果框架調整大小,組件將會移動。而是使用您的自定義面板來繪製圖像。

+0

完美謝謝!但是,有一件事我很困惑。六個按鈕不同的名稱?每個按鈕都會在我的代碼中使用一個actionlistener打開一個新面板,您可以使用此代碼爲我執行一個actionlistener,然後我將能夠看到您是如何做到的,並自行完成其他操作 –

+1

@ NatalieMcKnight,你不需要循環來創建按鈕秒。這只是一個簡短的例子來展示這個概念。在您的代碼中,您可以單獨創建按鈕,並將ActionListeners分別添加到每個按鈕。另外不要忘記閱讀我添加的「註釋」。 – camickr

+0

耶指出。 theres只是我得到的另一個小錯誤,當我點擊按鈕一個新的面板打開,應該包含背景圖像,但它不會顯示,你可以運行我的代碼,看看你是否可以找出原因?我更新了以上 –

-3

這裏的問題是,您的面板有設置到它的標準Layoutmanager。 與

contentPane.setLayout(null); 

嘗試禁用它,那麼你可以用button.setBountds(x,y,w,h);

周圍像您想要移動的按鈕,我總是直接畫上框,就像你不使用自定義面板,但我看到有這是更好的方法。

如果你寫

new JFrame.getContentPane().setLayout(null); 

你的框架得到默認的窗格中,仍然有它的佈局設置爲null。

不同之處在於,您可以直接添加到框架,而不是使用放置東西的面板。

frame.add(new Button().setBounds(x,y,w,h)); 

嘗試這個版本的代碼我試圖debugg:

Custom1 contentPane; 


//at first create and set the frame 
JFrame frame = new JFrame("Windowname goes here"); 
frame.setSize(1160, 700); 
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
frame.setLocationRelativeTo(null); 

//next add your custom panel 
contentPane = new Custom1(); 
frame.setContentPane(contentPane); 

//better add this to the panel instead of the frame 
JTextField textfield = new JTextField(20); 
frame.add(textfield); 

textfield.setBackground(Color.black); 
textfield.setForeground(Color.white); 

//you are using 2 panels with null layout here 
JPanel panel = new JPanel(); 
panel.setSize(frame.getHeight(),frame.getWidth());  //give the panel some bounds! 
panel.setOpaque(false); //unnecesary 
panel.setLayout(null); 
panel.setBackground(Color.BLACK); 
frame.add(panel); 

JButton button = new JButton("London"); 
JButton button1 = new JButton("Oxford"); 
JButton button2 = new JButton("Cambridge"); 

//you seemed to not have added these before 
JButton button4 = new JButton("Click"); 
JButton button5 = new JButton("Click"); 
JButton button6 = new JButton("Click"); 

button.setBounds(60, 400, 220, 30); 
button.setBackground(Color.black); 
button.setForeground(Color.WHITE); 

button1.setBounds(x,y,w,h);       // !!!! <------ 
button1.setBackground(Color.black); 
button1.setForeground(Color.WHITE); 

button2.setBounds(x,y,w,h);       // !!!! <------ 
button2.setBackground(Color.black); 
button2.setForeground(Color.WHITE); 

button4.setBounds(80, 50, 100, 30); 
button5.setBounds(x,y,w,h);       // !!!! <------ 
button6.setBounds(x,y,w,h);       // !!!! <------ 

panel.add(button); 
panel.add(button1); 
panel.add(button2); 
panel.add(button3); 
panel.add(button4); 
panel.add(button5); 
panel.add(button6); 

//SETVISIBLE ALWAYS GOES LAST 
frame.setVisible(true); 

我修補你的代碼,並在上面寫一些評論。 我標記了你需要爲按鈕設置邊界的地方! < ----,
你似乎已經做了這個button4,但不是其他的。你也應該在你的代碼中獲得更好的結構,我不想侮辱你,但我會稱之爲「意大利麪代碼」。 有一些東西,如果你遇到問題,例如你似乎沒有添加按鈕,但它們出現在你的照片中...

讓我知道它是否會工作後,玩弄代碼的邊界值和調查最終的錯誤。 此外,如果您向我發送了我可以監視的整個項目,但最好自己嘗試一下,如果別人做了你的工作,它不會給你任何東西。

好運和快樂編碼(͡°͜ʖ͡°)

+0

你可以將它添加到我的代碼中嗎?當我添加它。它刪除所有的按鈕,就好像它們不在那裏 –

+0

給我第二個 – clockw0rk

+3

(1-)不要使用空佈局。在包含按鈕的面板上使用GridLayout或GridBagLayout。 – camickr

相關問題