我在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