-1
我新,所以這個問題似乎令人難以置信的明顯改變面板...更改JButton的中心當其他按鈕被按下
我正在試圖改變Java中的邊界佈局,使中心按鈕是一個面板/ Jtextarea。當其他面板按下「Going *」*作爲方向時,該面板會發生反應。然後,當我按下一個新按鈕時,它會清除舊的,並將「Going **」**更改爲新的方向。我已經包括了當前的代碼和我正在尋找:)
/*
* 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();
}
});
}
}
對不起,我不知道在哪裏的原代碼,我打算把那..我真的很陌生 – Pyrexo 2015-02-24 17:00:33