[英]setVisible(true) on java Swing doesn't work
我正在使用 Java Swing进行一个“构建播客板表单”的项目。 我在同一个 package 中创建了多个“应用程序窗口”,但是为了在我单击按钮时打开一个框架,它不起作用。 我用过setVisible(true)
。 这是我的代码的一部分:
import java.awt.EventQueue;
public class Acceuil_After {
private JFrame Acceuil_After;
private JTextField textField2;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Acceuil_After window = new Acceuil_After();
window.Acceuil_After.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Acceuil_After() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
.........
//----JButton -----------------------------------------------------------------------------
JButton B_Acceuil_After1 = new JButton("Acceuil");
B_Acceuil_After1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
Acceuil_After after = new Acceuil_After();
after.setVisible(true);
}
});
B_Acceuil_After1.setBackground(new Color(77, 77, 77));
B_Acceuil_After1.setForeground(new Color(250, 234, 115));
B_Acceuil_After1.setFont(new Font("Century", Font.PLAIN, 17));
B_Acceuil_After1.setBorderPainted(false);
JButton podcast1 = new JButton("Nos podcasts");
podcast1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
Acceuil_After after = new Acceuil_After();
after.setVisible(true);
}
});
podcast1.setBackground(new Color(77, 77, 77));
podcast1.setFont(new Font("Century", Font.PLAIN, 17));
podcast1.setForeground(new Color(250, 234, 115));
podcast1.setBorderPainted(false);
JButton guide1 = new JButton("Guide d'écoute");
guide1.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
Acceuil_After after = new Acceuil_After();
after.setVisible(true);
}
});
guide1.setBackground(new Color(77, 77, 77));
guide1.setFont(new Font("Century", Font.PLAIN, 17));
guide1.setForeground(new Color(250, 234, 115));
guide1.setBorderPainted(false);
JButton B_inscrit11 = new JButton("S'inscrire");
B_inscrit11.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
Acceuil_After after = new Acceuil_After();
after.setVisible(true);
}
});
B_inscrit11.setForeground(new Color(77, 77, 77));
B_inscrit11.setFont(new Font("Arial", Font.PLAIN, 17));
B_inscrit11.setBackground(new Color(250, 234, 115));
B_inscrit11.setBorderPainted(false);
/* .............
............. */
public void setVisible(boolean b) {
// TODO Auto-generated method stub
}
}
我曾尝试使用addActionListener
而不是addMouseListener
但没有区别。
您好,谢谢您的回答。我已经修改了我的代码“我使用了 -class'- 而不是 -Application Window”。 -> 现在出现的问题是,除了没有我输入的任何化合物的空 window 之外,没有显示任何内容。这是我的新代码
public class Acceuil implements ActionListener{
JFrame acceuil = new JFrame();
JButton inscrire = new JButton("S'inscrire");
JButton connecter = new JButton("Se connecter");
JButton subscribe = new JButton("S'inscrire");
JButton B_acc = new JButton("Acceuil");
JButton B_pod = new JButton("Nos podcasts");
JButton B_gui = new JButton("Guide d'écoute");
JLabel lblNewLabel = new JLabel("COGITO");
JTextArea textt = new JTextArea();
JTextField textFieldd = new JTextField();
Acceuil()
{
//---JFrame ------------------------------------------------------
acceuil.getContentPane().setBackground(new Color(77, 77, 77));
acceuil.setTitle("Acceuil");
acceuil.setVisible(true);
acceuil.setLayout(null);
acceuil.setBounds(100, 100, 1297, 842);
acceuil.setSize(2000, 5000);
acceuil.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
acceuil.add(B_acc);
acceuil.add(B_gui);
acceuil.add(B_pod);
acceuil.add(acceuil);
acceuil.add(connecter);
acceuil.add(inscrire);
acceuil.add(lblNewLabel);
acceuil.add(subscribe);
acceuil.add(textFieldd);
acceuil.add(textt);
//----JButton ----------------------------------------------------
inscrire.setBackground(new Color(250, 234, 115));
inscrire.setForeground(Color.WHITE);
inscrire.setFont(new Font("Century", Font.PLAIN, 21));
inscrire.addActionListener(this);
connecter.setFont(new Font("Century", Font.PLAIN, 21));
connecter.setForeground(new Color(255, 255, 255));
connecter.setBackground(new Color(250, 234, 115));
connecter.addActionListener(this);
subscribe.setForeground(new Color(77, 77, 77));
subscribe.setFont(new Font("Arial", Font.PLAIN, 17));
subscribe.setBackground(new Color(250, 234, 115));
subscribe.setBorderPainted(false);
subscribe.addActionListener(this);
B_acc.setFont(new Font("Century", Font.PLAIN, 17));
B_acc.setForeground(new Color(250, 234, 115));
B_acc.setBackground(new Color(77, 77, 77));
B_acc.setBorderPainted(false);
B_acc.addActionListener(this);
B_pod.setFont(new Font("Century", Font.PLAIN, 17));
B_pod.setForeground(new Color(250, 234, 115));
B_pod.setBackground(new Color(77, 77, 77));
B_pod.setBorderPainted(false);
B_pod.addActionListener(this);
B_gui.setForeground(new Color(250, 234, 115));
B_gui.setBackground(new Color(77, 77, 77));
B_gui.setFont(new Font("Century", Font.PLAIN, 17));
B_gui.setBorderPainted(false);
B_gui.addActionListener(this);
//----JLabel ------------------------------------------------------
lblNewLabel.setFont(new Font("Century", Font.BOLD, 70));
lblNewLabel.setForeground(new Color(250, 234, 115));
lblNewLabel.setBackground(new Color(77, 77, 77));
//----JTextArea -------------------------------------------------
textt.setFont(new Font("Century", Font.PLAIN, 21));
textt.setForeground(Color.WHITE);
textt.setBackground(new Color(77, 77, 77));
textt.setText("Votre aventure philosophique et littéraire");
textt.setTabSize(20);
//----JTextField ------------------------------------------------
textFieldd.setText("Titres des podcasts de la semaine");
textFieldd.setForeground(Color.WHITE);
textFieldd.setBackground(new Color(77, 77, 77));
textFieldd.setFont(new Font("Century", Font.PLAIN, 21));
textFieldd.setColumns(10);
textFieldd.setBorder(null);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == inscrire)
{
acceuil.dispose();
Inscription inscriptionn = new Inscription();
}
if(e.getSource() == connecter)
{
acceuil.dispose();
Connexion guide = new Connexion();
}
if(e.getSource() == subscribe)
{
acceuil.dispose();
Inscription inscription = new Inscription();
}
if(e.getSource() == B_pod)
{
acceuil.dispose();
Inscription podcasts = new Inscription();
}
if(e.getSource() == B_gui)
{
acceuil.dispose();
Guide guide = new Guide();
}
}
}
我要感谢所有回答和帮助我的人。 最后,我解决了我遇到的问题。 这是我的新代码的一部分:
public class Acceuil implements ActionListener{
JFrame acceuil = new JFrame();
GroupLayout groupLayout = new GroupLayout(acceuil.getContentPane());
/* **************************************************************** */
Acceuil()
{
//---JFrame ----------------------------------------------------------
acceuil.getContentPane().setBackground(new Color(77, 77, 77));
acceuil.setTitle("Acceuil");
acceuil.setVisible(true);
acceuil.getContentPane().setLayout(groupLayout);
acceuil.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
acceuil.add(inscrire);
acceuil.add(lblNewLabel);
//.........................................
//----Grouplayout ----------------------------------------------------
groupLayout.setHorizontalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(72)
.addComponent(inscrire, GroupLayout.PREFERRED_SIZE,
158, GroupLayout.PREFERRED_SIZE)
.addPreferredGap(ComponentPlacement.RELATED, 742,
Short.MAX_VALUE)
//**********************************************************
);
groupLayout.setVerticalGroup(
groupLayout.createParallelGroup(Alignment.LEADING)
.addGroup(groupLayout.createSequentialGroup()
.addGap(71)
.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
.addComponent(inscrire)
.addComponent(connecter))
.addGap(32)
.addComponent(lblNewLabel)
.addGap(16)
//***************************************************
);
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == inscrire)
{
acceuil.dispose();
Inscription inscriptionn = new Inscription();
}
}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.