更改所有Tabbedpane面板Java Swing颜色的按钮动作 [英] Button action to change the color of all tabbedpane panel java swing
问题描述
我在选项卡式窗格中有2个选项卡(A和B). 在A中,我只写setBackground(Color.RED);
I have 2 tab(A and B) in tabbedpane. In A, I write only setBackground(Color.RED);
在B中,我放了一个按钮.像这样的代码:
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);
}
});
我想通过B的按钮操作更改A的颜色.但是我失败了. 我该如何解决这个问题?
I want to change the color of A from B's button action. But I failure. How can i solve this problem??
预先感谢...
我的问题仍然没有解决.我要发布整个代码::我使用了2个程序包:"ok","ok1". "ok"包含1个名为save.java的文件,代码为:
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"包含2个文件:A.java和B.java ..... A.java ::::::::
"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 ::::::::
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);
}
});
推荐答案
看着您的代码,看来您做错了.首先不要写这些行
Looking at your code, it seems you are doing it wrong. First of all don't write these lines
private JPanel panel1;
private JPanel panel2;
改为:
private A a = new A();
private B b = new B(a);
由于a和b,它们现在是Panels,因为它们扩展了JPanel类.
Since a and b, themselves are Panels now, as they extending JPanel class.
因此,现在将其添加到您的tabbedPane中:
So add this to your tabbedPane now :
jTabbedPane1.addTab("Tab 1", null, a/*This is your Panel1*/, "Just Panel");
jTabbedPane1.addTab("Tab 2", null, b/*This is your Panel2*/, "Button");
只需向您的B类添加一个JPanel变量,并按如下所示更改类B的构造函数:
Simply add a JPanel variable to your B class and change the constructor of your class B as follows :
JPanel panel1;
public B(JPanel panel)
{
pane1 = panel;
initComponents(); // make this method return void in it's definition, in both the classes.
}
现在在actionPerformed()方法内部执行此操作:
Now inside the actionPerformed() method do this :
jButton1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
panel1.setBackground(Color.RED);
}
});
这是从上次提交中修改的小程序,类似于您的情况:
Here is small program modified from the previous submission, that resembles your case :
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);
}
}
希望这会帮助您解释自己做错了什么.
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 :
这是您单击tab2的按钮以将tab1的背景颜色更改为蓝色时的第一个标签的图像:
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.
致谢
这篇关于更改所有Tabbedpane面板Java Swing颜色的按钮动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!