2015-02-24 79 views
-1

我新,所以這個問題似乎令人難以置信的明顯改變面板...更改JButton的中心當其他按鈕被按下

我正在試圖改變Java中的邊界佈局,使中心按鈕是一個面板/ Jtextarea。當其他面板按下「Going *」*作爲方向時,該面板會發生反應。然後,當我按下一個新按鈕時,它會清除舊的,並將「Going **」**更改爲新的方向。我已經包括了當前的代碼和我正在尋找:)

Swing Layout

/* 
* BorderLayoutDemo.java 
* 
*/ 
import javax.swing.*; 
import java.awt.BorderLayout; 
import java.awt.Container; 
import java.awt.Dimension; 


public class BorderLayoutDemo { 
public static boolean RIGHT_TO_LEFT = false; 

public static void addComponentsToPane(Container pane) { 

    if (!(pane.getLayout() instanceof BorderLayout)) { 
     pane.add(new JLabel("Container doesn't use BorderLayout!")); 
     return; 
    } 

    if (RIGHT_TO_LEFT) { 
     pane.setComponentOrientation(
       java.awt.ComponentOrientation.RIGHT_TO_LEFT); 
    } 


    JButton button = new JButton("Up"); 
    pane.add(button, BorderLayout.PAGE_START); 

    //Make the center component 400x400 
    //typical usage of BorderLayout. 

    button = new JButton("Going..."); 
    pane.setPreferredSize(new Dimension(400, 400)); 
    pane.add(button, BorderLayout.CENTER); 

    button = new JButton("Left"); 
    pane.add(button, BorderLayout.LINE_START); 

    button = new JButton("Down"); 
    pane.add(button, BorderLayout.PAGE_END); 

    button = new JButton("Right"); 
    pane.add(button, BorderLayout.LINE_END); 
} 

/** 
* Create the GUI and show it. For thread safety, 
* this method should be invoked from the 
* event dispatch thread. 
*/ 
private static void createAndShowGUI() { 

    //Create and set up the window. 
    JFrame frame = new JFrame("BorderLayoutDemo"); 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    //Set up the content pane. 
    addComponentsToPane(frame.getContentPane()); 
    //Use the content pane's default BorderLayout. No need for 
    //setLayout(new BorderLayout()); 
    //Display the window. 
    frame.pack(); 
    frame.setVisible(true); 
} 

public static void main(String[] args) { 
    /* Use an appropriate Look and Feel */ 
    try { 
     //UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); 
     UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); 
    } catch (UnsupportedLookAndFeelException ex) { 
     ex.printStackTrace(); 
    } catch (IllegalAccessException ex) { 
     ex.printStackTrace(); 
    } catch (InstantiationException ex) { 
     ex.printStackTrace(); 
    } catch (ClassNotFoundException ex) { 
     ex.printStackTrace(); 
    } 
    /* Turn off metal's use bold fonts */ 
    UIManager.put("swing.boldMetal", Boolean.FALSE); 

    //Schedule a job for the event dispatch thread: 
    //creating and showing this application's GUI. 
    javax.swing.SwingUtilities.invokeLater(new Runnable() { 
     public void run() { 
      createAndShowGUI(); 
     } 
    }); 
} 

}

回答

1

你需要在中心提供一個獨特的字段名將JButton的圖片。

goingButton = new JButton("Going..."); 
pane.add(goingButton, BorderLayout.CENTER); 

然後,您需要爲其他按鈕編寫一個動作偵聽器,用於更改goingButton的文本。以下是您如何設置JButton的文本。

button.addActionListener(new ActionListener() { 
     @Override 
     public void actionPerformed(ActionEvent event) { 
      goingButton.setText("Going up"); 
     }  
    }); 

編輯爲只給你修改後的代碼。如果別人爲你做你的工作,你不會學到任何東西。

下面是BorderLayoutDemo的屏幕截圖。

Border Layout Demo

而這裏的代碼,格式化,修改你:

package com.ggl.testing; 

/* 
* BorderLayoutDemo.java 
* 
*/ 
import java.awt.BorderLayout; 
import java.awt.Container; 
import java.awt.Dimension; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class BorderLayoutDemo { 
    private static final boolean RIGHT_TO_LEFT = false; 

    private JButton goingButton; 

    public void addComponentsToPane(Container pane) { 

     if (!(pane.getLayout() instanceof BorderLayout)) { 
      pane.add(new JLabel("Container doesn't use BorderLayout!")); 
      return; 
     } 

     if (RIGHT_TO_LEFT) { 
      pane.setComponentOrientation(java.awt.ComponentOrientation.RIGHT_TO_LEFT); 
     } 

     pane.setPreferredSize(new Dimension(400, 400)); 

     JButton button = new JButton("Up"); 
     button.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       goingButton.setText("Going up"); 
      } 
     }); 
     pane.add(button, BorderLayout.PAGE_START); 

     // Make the center component 400x400 
     // typical usage of BorderLayout. 

     goingButton = new JButton("Going..."); 
     goingButton.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       goingButton.setText("Going crazy"); 
      } 
     }); 
     pane.add(goingButton, BorderLayout.CENTER); 

     button = new JButton("Left"); 
     button.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       goingButton.setText("Going left"); 
      } 
     }); 
     pane.add(button, BorderLayout.LINE_START); 

     button = new JButton("Down"); 
     button.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       goingButton.setText("Going down"); 
      } 
     }); 
     pane.add(button, BorderLayout.PAGE_END); 

     button = new JButton("Right"); 
     button.addActionListener(new ActionListener() { 
      @Override 
      public void actionPerformed(ActionEvent e) { 
       goingButton.setText("Going right"); 
      } 
     }); 
     pane.add(button, BorderLayout.LINE_END); 
    } 

    /** 
    * Create the GUI and show it. For thread safety, this method should be 
    * invoked from the event dispatch thread. 
    */ 
    private void createAndShowGUI() { 

     // Create and set up the window. 
     JFrame frame = new JFrame("BorderLayoutDemo"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     // Set up the content pane. 
     addComponentsToPane(frame.getContentPane()); 
     // Use the content pane's default BorderLayout. No need for 
     // setLayout(new BorderLayout()); 
     // Display the window. 
     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static void main(String[] args) { 
     /* Use an appropriate Look and Feel */ 
     try { 
      // UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); 
      UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel"); 
     } catch (UnsupportedLookAndFeelException ex) { 
      ex.printStackTrace(); 
     } catch (IllegalAccessException ex) { 
      ex.printStackTrace(); 
     } catch (InstantiationException ex) { 
      ex.printStackTrace(); 
     } catch (ClassNotFoundException ex) { 
      ex.printStackTrace(); 
     } 
     /* Turn off metal's use bold fonts */ 
     UIManager.put("swing.boldMetal", Boolean.FALSE); 

     // Schedule a job for the event dispatch thread: 
     // creating and showing this application's GUI. 
     javax.swing.SwingUtilities.invokeLater(new Runnable() { 
      public void run() { 
       new BorderLayoutDemo().createAndShowGUI(); 
      } 
     }); 
    } 
} 
+0

對不起,我不知道在哪裏的原代碼,我打算把那..我真的很陌生 – Pyrexo 2015-02-24 17:00:33

相關問題