我有两个JFrames
newAccessLevels.java ,它有两个按钮“Level 1”“Level 2”和 newAccessPanel.java
我需要获得用户在accessPanel
上选择“1或2”的级别,以便我可以在 accessPanel.java 的标题中显示它,例如访问级别1,访问级别2.如何做到这一点。下面是示例代码,因此如果单击级别1,则newAccessPanel JFrame将以标题* ACCESS LEVEL 1 打开,反之亦然,级别2:
newAccessLevels.java
package securitySystem;
import java.awt.*;
import javax.swing.*;
import java.sql.*;
import java.awt.event.*;
public class newAccessLevels extends JFrame{
public static void main (String args[]){
newAccessLevels gui= new newAccessLevels ();
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setTitle("Access Levels");
gui.setSize(400,400);
gui.setLocationRelativeTo(null);
gui.setVisible(true);
}
JButton btnLevel1= new JButton("Levels 1");
JButton btnLevel2= new JButton("Level 2");
public newAccessLevels (){
setLayout (null);
btnLevel1.setBounds(120,70, 150, 30);
add(btnLevel1);
btnLevel2.setBounds(120,130, 150, 30);
add(btnLevel2);
}
public void calcButtons()
{
btnLevel1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
newAccessPanel gui =new newAccessPanel();
gui.setSize (360, 450);
gui.setLocationRelativeTo(null);
gui.setVisible(true);
dispose();
}
});
btnLevel2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
newAccessPanel gui =new newAccessPanel();
gui.setSize (360, 450);
gui.setLocationRelativeTo(null);
gui.setVisible(true);
dispose();
}
});
}
}
newAccessPanel.java
package securitySystem;
import java.awt.*;
import javax.swing.*;
import java.sql.*;
import java.awt.event.*;
public class newAccessPanel extends JFrame{
public static void main (String args[]){
newAccessPanel gui= new newAccessPanel ();
gui.setSize (360, 450);
gui.setLocationRelativeTo(null);
gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
gui.setVisible(true);
gui.setTitle("ACCESS LEVEL '1/2'");
//gui.setLayout(new BorderLayout());
//gui.setBackground(Color.BLACK);
}
}
答案 0 :(得分:5)
嗨,这是一种如何实现它的方法,只需要构建一个带有构造函数的新JFrame,该构造函数可以重现所需的参数。
第一个JFrame,其中有按钮
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JButton;
public class Frame1 extends JFrame{
private String mensaje;
private JButton btnHola;
private JButton btnAdios;
public Frame1() {
getContentPane().setLayout(null);
btnHola = new JButton("Hello");
btnHola.setBounds(63, 210, 89, 23);
getContentPane().add(btnHola);
btnHola.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
mensaje = Frame1.this.btnHola.getText();
Frame2 frame2 = new Frame2(mensaje);
}
});
btnAdios = new JButton("Bye");
btnAdios.setBounds(245, 210, 89, 23);
getContentPane().add(btnAdios);
btnAdios.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
mensaje = Frame1.this.btnAdios.getText();
Frame2 frame2 = new Frame2(mensaje);
}
});
}
public static void main(String[] args) {
Frame1 frame = new Frame1();
frame.setVisible(true);
}
}
第二个JFrame,收到消息。
import javax.swing.JFrame;
public class Frame2 extends JFrame {
public Frame2(String message) {
super();
setVisible(true);
setTitle(message);
}
}
我希望这对你有所帮助。 问候!