当我尝试创建JPopupMenu或实际上试图摆脱JPopupMenu时,我得到了我的代码行。我一直在寻找解决方案,所有这些都使用 Component .setVisible(false)或.setVisible(true);
但在我的情况下,我不会在我尝试的任何事情上工作。 总而言之,我的应用程序。正在工作,我将使用System.out.println(“工作正常或其他东西!”);所以我知道语句有变化,循环已经执行,ActionListener / Itemlistener等工作正常。 我有点使用“CreateGUI”创建一个主窗口,通过main调用([String [] args),有一个类Frame,然后放入Panel&用于控制布局的窗格。 我的框架除其他外,有一个JButton“vHent”,它调用PopKalender(),然后我的JPopupMenu显示在屏幕上。 在JPopupMenu“popUpKalenderWindow”中,我有一个JButton“kill”,“OK”(未使用)和一个MenuItem(“vMenu Tryk Her”)。
我的想法是从MenuItems读取一些状态,此时JButton“kill”是关闭JPopupMenu的。 但正如所说,似乎没有什么能关闭PopUpMenu!但是其他所有工作都是如此,当用System.out.println()检查它时,我会读出来,显示Loops,Construktor调用,Button / MenuItems等,正如他们所做的那样工作。 我试着直接输入:popUpKalenderWindow.setVisible(false)直接在我的ActionPerformedListener下,也只是PopKalender(),在popKalender(true / false)中尝试转换参数true / false然后创建一个if语句(如你所见)现在)它不会起作用。
(使用Eclipse,所以直接复制了代码行。有些评论是丹麦语(我很抱歉),但需要的地方是英语。还有一些评论是为了让我了解什么以及为什么这行编写!是的,CheckBox应该在ArrayList中并且用for(){}完成,所以这是Newbie解决方案! )。
所以任何人都可以在这里帮助我并告诉我为什么PopKalender()在按下“kill”时不会消失??
//package components;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JTextField;
public class Frame extends JPanel implements ItemListener, ActionListener {
private static final long _1L = 1L;
/**
*
*/
// Klargør CheckBoxes
JCheckBox tal1;
JCheckBox tal2;
JCheckBox tal3;
JCheckBox tal4;
JCheckBox tal5;
JCheckBox tal6;
JCheckBox tal7;
JCheckBox tal8;
JCheckBox tal9;
JCheckBox tal10;
JCheckBox tal11;
JCheckBox tal12;
JCheckBox tal13;
JCheckBox tal14;
JCheckBox tal15;
JCheckBox tal16;
JCheckBox tal17;
JCheckBox tal18;
JCheckBox tal19;
JCheckBox tal20;
JCheckBox tal21;
JCheckBox tal22;
JCheckBox tal23;
JCheckBox tal24;
JCheckBox tal25;
JCheckBox tal26;
JCheckBox tal27;
JCheckBox tal28;
JCheckBox tal29;
JCheckBox tal30;
JCheckBox tal31;
JCheckBox tal32;
JCheckBox tal33;
JCheckBox tal34;
JCheckBox tal35;
JCheckBox tal36;
//Klargør ArrayList til valgte tal
ArrayList<Integer> valg = new ArrayList<Integer>();
//Klargør knapper
JButton vOk; //til valgte tal Tekst: Tilføj
JButton vHent; //til at hente uge / kalender for valg af uge
JButton bRegn; //til at beregne ugen tal
//Klargør Tekster
JLabel labelValg = new JLabel("Du har valgt: "); //til valgte tal
JLabel labelTidNu = new JLabel("år-uge-dag-kl"); //til nuværende tid
JLabel labelValgTid = new JLabel("år-uge der skal skrives til"); //til valgte uge/år, der skal tilføjes Talvalg
JLabel labelUd_1 = new JLabel(" 1: "); //til udlæsning af Talforslag LabelUd_1 - LabelUd_10
JLabel labelUd_2 = new JLabel(" 2: ");
JLabel labelUd_3 = new JLabel(" 3: ");
JLabel labelUd_4 = new JLabel(" 4: ");
JLabel labelUd_5 = new JLabel(" 5: ");
JLabel labelUd_6 = new JLabel(" 6: ");
JLabel labelUd_7 = new JLabel(" 7: ");
JLabel labelUd_8 = new JLabel(" 8: ");
JLabel labelUd_9 = new JLabel(" 9: ");
JLabel labelUd_10 = new JLabel("10: ");
public Frame(){
super(new BorderLayout());
//Opret Paneler til under paneler
//Opret Top, left, center, right, buttom Panel
JPanel topPanel = new JPanel(); //Indsæt BorderLayout efter opstilling !
JPanel leftPanel = new JPanel();
JPanel centerPanel = new JPanel();
JPanel rightPanel = new JPanel();
JPanel bottomPanel = new JPanel();
//Set internt Layout for top, left, right panel
topPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
leftPanel.setLayout(new GridLayout(4, 0));
rightPanel.setLayout(new GridLayout(2, 0));
//Set størrelse på paneler
topPanel.setPreferredSize(new Dimension(900, 50));
leftPanel.setPreferredSize(new Dimension(300, 500));
centerPanel.setPreferredSize(new Dimension(300, 500));
rightPanel.setPreferredSize(new Dimension(300, 500));
bottomPanel.setPreferredSize(new Dimension(900, 50));
//tilføj paneler til frame'en og placer dem i layout'et
add(topPanel, BorderLayout.PAGE_START);
add(leftPanel, BorderLayout.LINE_START);
add(centerPanel, BorderLayout.CENTER);
add(rightPanel, BorderLayout.LINE_END);
add(bottomPanel, BorderLayout.PAGE_END);
//Opret Checkboxes
tal1 = new JCheckBox("1");
tal2 = new JCheckBox("2");
tal3 = new JCheckBox("3");
tal4 = new JCheckBox("4");
tal5 = new JCheckBox("5");
tal6 = new JCheckBox("6");
tal7 = new JCheckBox("7");
tal8 = new JCheckBox("8");
tal9 = new JCheckBox("9");
tal10 = new JCheckBox("10");
tal11 = new JCheckBox("11");
tal12 = new JCheckBox("12");
tal13 = new JCheckBox("13");
tal14 = new JCheckBox("14");
tal15 = new JCheckBox("15");
tal16 = new JCheckBox("16");
tal17 = new JCheckBox("17");
tal18 = new JCheckBox("18");
tal19 = new JCheckBox("19");
tal20 = new JCheckBox("20");
tal21 = new JCheckBox("21");
tal22 = new JCheckBox("22");
tal23 = new JCheckBox("23");
tal24 = new JCheckBox("24");
tal25 = new JCheckBox("25");
tal26 = new JCheckBox("26");
tal27 = new JCheckBox("27");
tal28 = new JCheckBox("28");
tal29 = new JCheckBox("29");
tal30 = new JCheckBox("30");
tal31 = new JCheckBox("31");
tal32 = new JCheckBox("32");
tal33 = new JCheckBox("33");
tal34 = new JCheckBox("34");
tal35 = new JCheckBox("35");
tal36 = new JCheckBox("36");
//Set default
tal1.setSelected(false);
tal2.setSelected(false);
tal3.setSelected(false);
tal4.setSelected(false);
tal5.setSelected(false);
tal6.setSelected(false);
tal7.setSelected(false);
tal8.setSelected(false);
tal9.setSelected(false);
tal10.setSelected(false);
tal11.setSelected(false);
tal12.setSelected(false);
tal13.setSelected(false);
tal14.setSelected(false);
tal15.setSelected(false);
tal16.setSelected(false);
tal17.setSelected(false);
tal18.setSelected(false);
tal19.setSelected(false);
tal20.setSelected(false);
tal21.setSelected(false);
tal22.setSelected(false);
tal23.setSelected(false);
tal24.setSelected(false);
tal25.setSelected(false);
tal26.setSelected(false);
tal27.setSelected(false);
tal28.setSelected(false);
tal29.setSelected(false);
tal30.setSelected(false);
tal31.setSelected(false);
tal32.setSelected(false);
tal33.setSelected(false);
tal34.setSelected(false);
tal35.setSelected(false);
tal36.setSelected(false);
//opret en ItemListener for Checkboxes
tal1.addItemListener(this);
tal2.addItemListener(this);
tal3.addItemListener(this);
tal4.addItemListener(this);
tal5.addItemListener(this);
tal6.addItemListener(this);
tal7.addItemListener(this);
tal8.addItemListener(this);
tal9.addItemListener(this);
tal10.addItemListener(this);
tal11.addItemListener(this);
tal12.addItemListener(this);
tal13.addItemListener(this);
tal14.addItemListener(this);
tal15.addItemListener(this);
tal16.addItemListener(this);
tal17.addItemListener(this);
tal18.addItemListener(this);
tal19.addItemListener(this);
tal20.addItemListener(this);
tal21.addItemListener(this);
tal22.addItemListener(this);
tal23.addItemListener(this);
tal24.addItemListener(this);
tal25.addItemListener(this);
tal26.addItemListener(this);
tal27.addItemListener(this);
tal28.addItemListener(this);
tal29.addItemListener(this);
tal30.addItemListener(this);
tal31.addItemListener(this);
tal32.addItemListener(this);
tal33.addItemListener(this);
tal34.addItemListener(this);
tal35.addItemListener(this);
tal36.addItemListener(this);
//Sæt CheckBoxes i 6 kolonner i et Panel
JPanel checkPanel = new JPanel(new GridLayout(0, 6));
checkPanel.add(tal1);
checkPanel.add(tal2);
checkPanel.add(tal3);
checkPanel.add(tal4);
checkPanel.add(tal5);
checkPanel.add(tal5);
checkPanel.add(tal6);
checkPanel.add(tal7);
checkPanel.add(tal8);
checkPanel.add(tal9);
checkPanel.add(tal10);
checkPanel.add(tal11);
checkPanel.add(tal12);
checkPanel.add(tal13);
checkPanel.add(tal14);
checkPanel.add(tal15);
checkPanel.add(tal16);
checkPanel.add(tal17);
checkPanel.add(tal18);
checkPanel.add(tal19);
checkPanel.add(tal20);
checkPanel.add(tal21);
checkPanel.add(tal22);
checkPanel.add(tal23);
checkPanel.add(tal24);
checkPanel.add(tal25);
checkPanel.add(tal26);
checkPanel.add(tal27);
checkPanel.add(tal28);
checkPanel.add(tal29);
checkPanel.add(tal30);
checkPanel.add(tal31);
checkPanel.add(tal32);
checkPanel.add(tal33);
checkPanel.add(tal34);
checkPanel.add(tal35);
checkPanel.add(tal36);
//Set checkpanel størrelse og tilføj det til leftPanel med en ramme
checkPanel.setPreferredSize(new Dimension(290, 290));
leftPanel.add(checkPanel);
setBorder(BorderFactory.createEmptyBorder(20,20,20,20));
//leftPanel med valgte tal
leftPanel.add(labelValg);
labelValg.setPreferredSize(new Dimension(290, 25));
//Tilføj tekster
//topPanel m. nuværende tid
topPanel.add(labelTidNu);
labelTidNu.setPreferredSize(new Dimension(210, 25));
//Hjælpe funktioner til ur
Thread t = new Kalender(labelTidNu); //Bruges til at lave en tråd Thread for Kalender
t.start(); //Tråden t startes og kan kontrolleres med Thread.sleep(1000) = pause
//klargør Knapper til: valgte tal
vOk = new JButton("Tilføj Tal");
vOk.setPreferredSize(new Dimension(100, 50));
vOk.setActionCommand("vOk"); //Set ActionCommand til genkendelse
vOk.addActionListener(this); //Tilføj en Action Listener
//hent uge / kalender
vHent = new JButton("Hent Uge");
vHent.setActionCommand("vHent");
vHent.addActionListener(this);
vHent.setPreferredSize(new Dimension(100, 50));
//Beregn ugens tal
bRegn = new JButton("Beregn");
bRegn.setActionCommand("bRegn");
bRegn.addActionListener(this);
bRegn.setPreferredSize(new Dimension(100, 50));
//Opret et knap Panel
JPanel knapPanel = new JPanel();
knapPanel.setLayout(new BoxLayout(knapPanel, BoxLayout.Y_AXIS));
// knapPanel.setPreferredSize(new Dimension(290, 300));
//opret layoutpaneler med flowlayoutX til knapper og tekst
//layoutPanel1
JPanel layoutPanel1 = new JPanel();
layoutPanel1.setLayout(new FlowLayout(FlowLayout.LEFT));
layoutPanel1.setPreferredSize(new Dimension(290, 150));
//layoutPanel2
JPanel layoutPanel2 = new JPanel();
layoutPanel2.setLayout(new FlowLayout(FlowLayout.RIGHT));
layoutPanel2.setPreferredSize(new Dimension(290, 150));
//Tilføj knapper / tekster
layoutPanel1.add(vHent);
layoutPanel1.add(labelValgTid);
layoutPanel2.add(vOk);
knapPanel.add(layoutPanel1);
// knapPanel.add(fillPanel1);
knapPanel.add(layoutPanel2);
leftPanel.add(knapPanel);
setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
//Opret panel til udlæsning
JPanel listePanel = new JPanel();
listePanel.setLayout(new BoxLayout(listePanel, BoxLayout.Y_AXIS));
listePanel.setPreferredSize(new Dimension(280, 300));
//Opret Panel til Knap
JPanel knapPanel2 = new JPanel();
knapPanel2.setPreferredSize(new Dimension(280, 150));
knapPanel2.setLayout(new FlowLayout(FlowLayout.RIGHT));
//Tilføj lister / knap
listePanel.add(labelUd_1);
listePanel.add(labelUd_2);
listePanel.add(labelUd_3);
listePanel.add(labelUd_4);
listePanel.add(labelUd_5);
listePanel.add(labelUd_6);
listePanel.add(labelUd_7);
listePanel.add(labelUd_8);
listePanel.add(labelUd_9);
listePanel.add(labelUd_10);
rightPanel.add(listePanel);
knapPanel2.add(bRegn);
rightPanel.add(knapPanel2);
} //Frame end
//Lyt til Checkboxes, send resultatet til Valgtjek og opdater display / LabelValg
public void itemStateChanged(ItemEvent e){
// int index = 0;
Object kilde = e.getItemSelectable();
if(kilde == tal1){
Valgtjek(1);
}else if(kilde == tal2){
Valgtjek(2);
}else if(kilde == tal3){
Valgtjek(3);
}else if(kilde == tal4){
Valgtjek(4);
}else if(kilde == tal5){
Valgtjek(5);
}else if(kilde == tal6){
Valgtjek(6);
}else if(kilde == tal7){
Valgtjek(7);
}else if(kilde == tal8){
Valgtjek(8);
}else if(kilde == tal9){
Valgtjek(9);
}else if(kilde == tal10){
Valgtjek(10);
}else if(kilde == tal11){
Valgtjek(11);
}else if(kilde == tal12){
Valgtjek(12);
}else if(kilde == tal13){
Valgtjek(13);
}else if(kilde == tal14){
Valgtjek(14);
}else if(kilde == tal15){
Valgtjek(15);
}else if(kilde == tal16){
Valgtjek(16);
}else if(kilde == tal17){
Valgtjek(17);
}else if(kilde == tal18){
Valgtjek(18);
}else if(kilde == tal19){
Valgtjek(19);
}else if(kilde == tal20){
Valgtjek(20);
}else if(kilde == tal21){
Valgtjek(21);
}else if(kilde == tal22){
Valgtjek(22);
}else if(kilde == tal23){
Valgtjek(23);
}else if(kilde == tal24){
Valgtjek(24);
}else if(kilde == tal25){
Valgtjek(25);
}else if(kilde == tal26){
Valgtjek(26);
}else if(kilde == tal27){
Valgtjek(27);
}else if(kilde == tal28){
Valgtjek(28);
}else if(kilde == tal29){
Valgtjek(29);
}else if(kilde == tal30){
Valgtjek(30);
}else if(kilde == tal31){
Valgtjek(31);
}else if(kilde == tal32){
Valgtjek(32);
}else if(kilde == tal33){
Valgtjek(33);
}else if(kilde == tal34){
Valgtjek(34);
}else if(kilde == tal35){
Valgtjek(35);
}else if(kilde == tal36){
Valgtjek(36);
}
//Opdater og udlæs valgte
String s = valg.toString();
labelValg.setText("Du har valgt: " +s);
}
//Lyt til Knapper //Button Listener
public void actionPerformed(ActionEvent e){
Object kilde = e.getActionCommand();
if(kilde == "vOk"){
System.out.println("Tillykke vOk er Ramt");
}else{
if(kilde == "vHent"){
PopKalender(true); //Call PopUpWindow
System.out.println("Tillykke vHent er ramt"); //This works and "PopKalender" shows on screen
}else {
if(kilde == "bRegn"){
System.out.println("Tillykke der beregnes");
}
else{
if(kilde == "Kill"){
PopKalender(false);
System.out.println("KILL WINDOW"); //This read out works fine, but "PopKalender" dosen't disappear
// System.exit( 0 ); //Also work but NOT as intended !!! :D
}
}
}
} }
//Ur til topPanel
//Der oprettes en klasse
class Kalender extends Thread {
JLabel labelUrOpdater;
Kalender(JLabel labelTidNu){
labelUrOpdater = labelTidNu;
}
//Opret variabler til nutid år, måned, dag i måned, uge, dag, timer, minutter, sek
int year;
int month;
String monthName;
int date;
int dayOfMonth;
int weekOfYear;
int dayOfWeek;
int hour;
int minute;
int second;
String[] måneder = {"Januar", "Februar", "Marts", "April", "Maj", "Juni", "Juli", "August", "September", "Oktober", "November", "December" };
public void run() {
Calendar ur = new GregorianCalendar();
for(;;){
Date trialTime = new Date();
ur.setTime(trialTime);
year = ur.get(Calendar.YEAR);
month = ur.get(Calendar.MONTH);
date = ur.get(Calendar.DATE);
dayOfMonth = ur.get(Calendar.DAY_OF_MONTH);
weekOfYear = ur.get(Calendar.WEEK_OF_YEAR);
dayOfWeek = ur.get(Calendar.DAY_OF_WEEK);
hour = ur.get(Calendar.HOUR_OF_DAY);
minute = ur.get(Calendar.MINUTE);
second = ur.get(Calendar.SECOND);
monthName = måneder[month];
//Sæt 0 foran 0-9 i sekunder / minutter
if((minute <= 9) || (second <= 9)){
if(minute <= 9){
labelTidNu.setText( year + ". " + dayOfMonth + " " + monthName + " " + "Uge: " + weekOfYear + " " + "Kl: " + hour + ":" + "0" + minute + ":" + second);
}else{
if(second <= 9){
labelTidNu.setText( year + ". " + dayOfMonth + " " + monthName + " " + "Uge: " + weekOfYear + " " + "Kl: " + hour + ":" + minute + ":" + "0" + second);
}
}
}
else{
labelTidNu.setText( year + ". " + dayOfMonth + " " + monthName + " " + "Uge: " + weekOfYear + " " + "Kl: " + hour + ":" + minute + ":" + second);
// labelTidNu.repaint();
}
try { Thread.sleep(1000);
} catch(InterruptedException e){
System.out.println("Thread Error");
}
}
}
}
//Tal Checker: Se om de valgte tal er på listen, fjern eller tilføj. Call fra ItemStateChange
public void Valgtjek(int tjek) {
boolean v = valg.contains(tjek);
if(v){
int i = valg.indexOf(tjek);
valg.remove(i);
System.out.println("Fjernede");
}
else{
valg.add(tjek);
System.out.println("valgte :" + tjek);
}
}
//ActionListener for PopUpKalender "PopKalender" / ActionListener f. JPopupMenu
class PopKalValg implements ActionListener{
public void actionPerformed(ActionEvent actionEvent){
System.out.println("Valgt" + actionEvent.getActionCommand());
}
}
//Pop-up til kalender valg (Popup Menu for my Pick Calender
public void PopKalender(boolean pState){
boolean pTest = false;
JPopupMenu popUpKalenderWindow = new JPopupMenu();
// popUpKalenderWindow.setDefaultCloseOperation();
popUpKalenderWindow.setPreferredSize(new Dimension(400, 400));
popUpKalenderWindow.setLocation(700, 300);
ActionListener kalActionValg = new PopKalValg(); //ActionListener to objects inside the JPopupMenu
JPanel popPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); //Panel to hold objects and control layout
JButton dOk = new JButton("OK"); //Not in use, just show
JMenuItem vMenu = new JMenuItem("vMenu Tryk Her"); //test Item to learn JMenuItem
vMenu.addActionListener( kalActionValg);
popPanel.add(vMenu);
JButton kill = new JButton("kill"); //In Use, supposed to shut down JPopupMenu
kill.setActionCommand("Kill");
kill.addActionListener(this);
popPanel.add(dOk);
popPanel.add(kill);
popUpKalenderWindow.add(popPanel);
if(pTest != pState){
popUpKalenderWindow.setVisible(true); //works fine
pTest = true;
System.out.println(pTest); // works fine, state has changed
}else{
// getRootPane().setVisible(false); //Dosen't work
// popPanel.setVisible(false); Dosen't Work
popUpKalenderWindow.setVisible(false); // Dosen't work
System.out.println("Pop set visible false"); //Do work, so loop statement has been reached !!!
}
}
private static void createAndShowGUI(){
//Create and set up window
JFrame frame = new JFrame("Mit Program");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Afslut Runtime ved vindue luk
frame.setPreferredSize(new Dimension(900, 600)); //Set størrelse på vinduet
/* //Create background, put background in ContentPane place it Center
JLabel bckGnd = new JLabel("");
frame.getContentPane().add(bckGnd, BorderLayout.CENTER);
*/
//Opret et Ground Panel til panels
JComponent groundPanel = new Frame();
groundPanel.setOpaque(true); //content Panes skal være opaque
frame.getContentPane().add(groundPanel); //Tilføj GroundPanel til frame
//Display Window
frame.pack();
frame.setVisible(true);
}
//SerialVersion ???
private static final long serialVersionUID = _1L;
public static void main(String[] args) {
// Schedule a job for event-dispatching thread:
//Creating and showing this Application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable(){
public void run(){
createAndShowGUI();
}
});
}
}
答案 0 :(得分:1)
说实话,你的代码很糟糕。无论如何,使kill按钮工作的最短方法之一是向它添加一个ActionListener,并实现在对该按钮执行操作时会发生什么。
而不是此行kill.addActionListener(this);
使用此:
kill.addActionListener(new ActionListener()
{
public void actionPerformed ( ActionEvent e )
{
popUpKalenderWindow.setVisible( false );
}
});
要使上述代码生效,请将popUpKalenderWindow
标记为final
。
final JPopupMenu popUpKalenderWindow = new JPopupMenu();