2012-01-16 60 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