简体   繁体   中英

how to get jpanel components in another jframe class

I have a JFrame call availabilty details. In this frame I have a JPanel and a button. In that JPanel I have two toggle buttons. When I click the button it goes to a new JFrame call reservation. I need java code for this. That when I select two toogle buttons and press the button, it wants to display the toggle button name and JPanel name in reservation.. here is my availabiltydetails frame.

在此处输入图片说明

I want that toogle button name and jpanel name wants to display in reservation frame when I clicked the button.

and here this is the code i already typed in availabilitydetails on add button event

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    Reservation r = new Reservation();
    Reservation.bf.getSelectedItems(this.b.getValue());
    r.setVisible(true);
    this.dispose();
}

is the code is wrong??

The Code Like This:

JFrameT.java:

import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class JFrameT extends JFrame {

        private JButton testButton;
        private JPanel panelMain;
        private JPanelOne panel;

        public JFrameT() {

                // setting up JFrame
                setLayout(null);
                setPreferredSize(new Dimension(420, 90));
                setResizable(false);
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                // creating main JPanel (white)
                panelMain = new JPanel();
                panelMain.setBackground(Color.WHITE);
                panelMain.setBounds(0, 0, 420, 90);
                panelMain.setPreferredSize(new Dimension(200, 40));
                add(panelMain);

                // creating JButton in the main JPanel (white)
                testButton = new JButton("Button from main class");
                panelMain.add(testButton);

                // creating new JPanelOne object from JPanelOne class containing black JPanel
                panel = new JPanelOne();

                // adding black JPanel to main JPanel (white)
                panelMain.add(panel);

                pack();

        }

        public static void main(String[] arguments) {

                //Creat JFrame object and setting it visible
                JFrameT frame = new JFrameT();
                frame.setVisible(true);

        }

}

JPanelOne.java:

import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class JPanelFirst extends JPanel
{

        public JPanelFirst()
        {
                // setting up black JPanel
                JPanel panel = new JPanel();
                panel.setPreferredSize(new Dimension(220, 40));
                panel.setBackground(Color.BLACK);

                // creating button on external JPanel
                JButton button = new JButton("Button (+JPanel) from external class");

                // adding button to the black JPanel
                panel.add(button);

                // adding blackJPanel
                add(panel);
        }
}

You can find it helpful more in this problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM