2012-01-16 65 views
0

我在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); 
     } 
    }); 
+0

請包括一個[sscce](http://www.sscce.org)來演示您的問題。 – mre 2012-01-16 13:19:09

+0

感謝您的回覆。你能給我一個SSCCE的例子嗎? – Rounak 2012-01-16 13:28:05

+0

這是不言自明的排序的,但這裏的一個[示例](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

回答

0

你不應該創建一個新的插件可使用A,但使用包含在選項卡式窗格中的A實例。您可以通過搜索選項卡式窗格此A實例,或A實例傳遞給B創建B

+0

對不起,我不明白該怎麼做。 – Rounak 2012-01-16 13:19:45

+0

該'甲一個新= A()'應該不存在在'B'類的代碼。發佈SSCCE,我們可以指出你出錯的地方 – Robin 2012-01-16 13:31:54

1

參考TabColors,這與配合的突出和內容的顏色開始時要麼這一點,以下修改TabContent構造函數添加一個按鈕導致所有窗格使用相同的顏色。在你的代碼

private TabContent(final int i, Color color) { 
    setOpaque(true); 
    setBackground(color); 
    add(new JLabel("Tab content " + String.valueOf(i))); 
    add(new JButton(new AbstractAction("Use my color") { 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      for (int j = 0; j < pane.getTabCount(); j++) { 
       pane.getComponentAt(j).setBackground(
        pane.getBackgroundAt(i)); 
      } 
     } 
    })); 
} 
1

看,看來你是做錯了。首先,不寫這幾行

private JPanel panel1; 
private JPanel panel2; 

,而不是寫:

private A a = new A(); 
private B b = new B(a); 

由於A和B,本身面板現在,因爲他們擴展JPanel類。

所以現在添加到您的選項卡窗格:

jTabbedPane1.addTab("Tab 1", null, a/*This is your Panel1*/, "Just Panel"); 
jTabbedPane1.addTab("Tab 2", null, b/*This is your Panel2*/, "Button"); 

一個JPanel變量只需添加到您的B級,並改變你的B類的構造函數如下:

JPanel panel1; 

public B(JPanel panel) 
{ 
    pane1 = panel; 
    initComponents(); // make this method return void in it's definition, in both the classes. 
} 

現在的actionPerformed內()方法執行此操作:

jButton1.addActionListener(new ActionListener() { 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      // TODO Auto-generated method stub 
      panel1.setBackground(Color.RED); 
     } 
    }); 

這裏是從p revious提交,類似於你的情況:

import java.awt.*; 
import java.awt.event.*; 

import javax.swing.*; 

public class TabbedPaneExample extends JPanel 
{ 
    private Panel1 panel1; 
    private Panel2 panel2; 

    public TabbedPaneExample() 
    { 
     super(new GridLayout(1, 1)); 

     JTabbedPane tabbedPane = new JTabbedPane(); 

     //panel1 = getPanel("Panel Number 1"); 
     panel1 = new Panel1("Panel Number 1"); 
     tabbedPane.addTab("Tab 1", null, panel1, "Just Panel"); 
     tabbedPane.setMnemonicAt(0, KeyEvent.VK_1); 

     //panel2 = getPanelWithButton("COLOR"); 
     panel2 = new Panel2("COLOR", panel1); 
     tabbedPane.addTab("Tab 2", null, panel2, "Button"); 
     tabbedPane.setMnemonicAt(1, KeyEvent.VK_2); 

     add(tabbedPane); 

     tabbedPane.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT); 
    } 

    private static void createAndDisplayGUI() 
    { 
     JFrame frame = new JFrame("Tabbed Pane Example"); 
     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 

     frame.add(new TabbedPaneExample(), BorderLayout.CENTER); 

     frame.pack(); 
     frame.setVisible(true); 
    } 

    public static void main(String... args) 
    { 
     SwingUtilities.invokeLater(new Runnable() 
      { 
       public void run() 
       { 
        createAndDisplayGUI(); 
       } 
      }); 
    } 
} 

class Panel1 extends JPanel 
{ 
    public JLabel label; 
    public Panel1(String text) 
    {  
     label = new JLabel(text); 
     label.setHorizontalAlignment(JLabel.CENTER); 
     setLayout(new GridLayout(1, 1)); 
     setBackground(Color.RED); 
     add(label); 
    } 
} 

class Panel2 extends JPanel 
{ 
    public JButton button; 
    public JPanel panel1; 

    public Panel2(String text, JPanel panel) 
    { 
      panel1 = panel; 
     button = new JButton(text); 
     button.addActionListener(new ActionListener() 
      { 
       public void actionPerformed(ActionEvent ae) 
       { 
        panel1.setBackground(Color.BLUE); 
       } 
      }); 
     setLayout(new GridLayout(1, 1)); 
     add(button); 
    } 
} 

希望這將有助於你解釋你在做什麼錯。

這裏是因爲它是啓動的節目的圖像:

AT START

這裏是所述第二接線片與按鈕圖像:

TAB WITH BUTTON ON IT

這裏是圖像第一個選項卡,你點擊TAB2的按鈕來改變TAB1的藍色背景:

BACKGROUND COLOR OF TAB1 CHANGED

希望這可以幫助你在你的努力。

Regards