我在tabbedpane中有2個選項卡(A和B)。 在A中,我只寫了setBackground(Color.RED);更改所有tabbedpane面板java swing的顏色按鈕操作
在B中,我放了一個按鈕。代碼如下:
A a=new A();
jButton1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
a.setBackground(Color.BLUE);
}
});
我想從B的按鈕動作改變A的顏色。但我失敗了。 我該如何解決這個問題?
在此先感謝...
還是我的問題沒有解決。我發佈了整個代碼::我使用了2個包:「ok」,「ok1」。 「OK」 包含1個文件名爲save.java和代碼是:
public class Save extends javax.swing.JFrame {
private JPanel panel1;
private JPanel panel2;A a=new A();B b=new B();
public Save() {
initComponents();
}
//GEN-BEGIN:initComponents
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
panel1=a.initComponents();
panel2=b.initComponents();
jTabbedPane1 = new javax.swing.JTabbedPane();
jScrollPane1 = new javax.swing.JScrollPane();
jScrollPane2 = new javax.swing.JScrollPane();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jTabbedPane1.addTab("Tab 1", null, panel1, "Just Panel");
jTabbedPane1.setMnemonicAt(0, KeyEvent.VK_1);
jTabbedPane1.addTab("Tab 2", null, panel2, "Button");
jTabbedPane1.setMnemonicAt(1, KeyEvent.VK_2);
「OK1」 包含2個文件:A.java和B.java ..... A.java ::: :::::
public class A extends javax.swing.JPanel {
/** Creates new form A */
public A() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
//GEN-BEGIN:initComponents
// <editor-fold defaultstate="collapsed" desc="Generated Code">
public JPanel initComponents() {
jPanel11 = new javax.swing.JPanel();
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
jPanel11);
jPanel11.setLayout(jPanel1Layout);
B.java ::::::::
public class B extends javax.swing.JPanel {
A a = new A();
/** Creates new form B */
public B() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
//GEN-BEGIN:initComponents
// <editor-fold defaultstate="collapsed" desc="Generated Code">
public JPanel initComponents() {
jPanel1 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();
jButton1.setText("Action");
jButton1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
a.jPanel11.setBackground(Color.RED);
}
});
請包括一個[sscce](http://www.sscce.org)來演示您的問題。 – mre 2012-01-16 13:19:09
感謝您的回覆。你能給我一個SSCCE的例子嗎? – Rounak 2012-01-16 13:28:05
這是不言自明的排序的,但這裏的一個[示例](http://stackoverflow.com/questions/7028780/how-to-add-20-pixels-of-white-at-the-top-of-的存在的圖像文件/ 7028977#7028977)。 – mre 2012-01-16 13:31:31