更改所有选项卡窗格面板 java swing 的颜色的按钮操作
我在选项卡窗格中有 2 个选项卡(A 和 B)。 在A中,我只写了setBackground(Color.RED);
在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);
}
});
I have 2 tab(A and B) in tabbedpane.
In A, I write only setBackground(Color.RED);
In B, I put a Button. and the codes likes that:
A a=new A();
jButton1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
a.setBackground(Color.BLUE);
}
});
I want to change the color of A from B's button action. But I failure.
How can i solve this problem??
Thanks in advance...
still my problem not solved. I am posting the whole code:: I used 2 package: "ok","ok1". "ok" contains 1 file named save.java and the code is:
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" contains 2 file: A.java and 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);
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
参考
TabColors
,它以匹配选项卡和内容颜色开头,修改后的TabContent
构造函数添加一个按钮,使所有窗格使用相同的颜色。Referring to
TabColors
, which starts with matching tab and content colors, the following modifiedTabContent
constructor adds a button that causes all panes to use the same color.看看你的代码,你似乎做错了。首先,不要写这些行,
而是写:
由于 a 和 b,它们现在是面板,因为它们扩展了 JPanel 类。
因此,现在将其添加到您的 tabbedPane 中:
只需将 JPanel 变量添加到您的 B 类中,并按如下所示更改类 B 的构造函数:
现在在 actionPerformed() 方法中执行以下操作:
这是根据之前提交修改的小程序,类似于您的情况:
希望这能帮助您解释您做错了什么。
这是程序启动时的图像:
这是带有按钮的第二个选项卡的图像:
< img src="https://i.sstatic.net/Jbdxv.png" alt="TAB WITH BUTTON ON IT">
这是当您单击 tab2 的按钮以更改 tab1 的背景颜色时第一个选项卡的图像蓝色:
希望这可以帮助您。
问候
Looking at your code, it seems you are doing it wrong. First of all don't write these lines
instead write :
Since a and b, themselves are Panels now, as they extending JPanel class.
So add this to your tabbedPane now :
Simply add a JPanel variable to your B class and change the constructor of your class B as follows :
Now inside the actionPerformed() method do this :
Here is small program modified from the previous submission, that resembles your case :
Hope this will help you explain what you doing wrong.
Here is the image of the program as it is started :
Here is the image of the second tab with button :
Here is the image of the first tab as you click on tab2's button to change tab1's background color to blue :
Hope this might help you in your endeavour.
Regards
您不应创建
A
的新实例,而应使用选项卡式窗格中包含的A
实例。您可以通过在选项卡式窗格中搜索此A
实例来实现此目的,或者在创建B< 时将
A
实例传递给B
/代码>You should not create a new instance of
A
, but use theA
instance which was included in the tabbed pane. You can either to this by searching your tabbed pane for thisA
instance, or pass theA
instance toB
when creatingB