2017-05-02 6 views
0

몇 가지 문제가 있습니다.
1. 나가기 전에 메시지를 보여주고 싶습니다. 버튼과 메시지 표시를 클릭하고 0.5 초 후 프로그램 종료 후 다음 프로그램을 엽니 다.
2. 색상을 변경하고 싶습니다. 첫 번째 앱 색상은 정확하지만 다음 색상은 기본입니다.

수정할 수 있습니까? 또는 내 단계에이를 수행하는 방법을 알려줄 수 있습니까? 어떤 도움을 주셔서 감사합니다 :). 첫 번째 프로그램의

코드 :JLabel이 표시되지 않습니다.

import java.awt.Color; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.util.concurrent.TimeUnit; 

import javax.swing.JButton; 
import javax.swing.JDialog; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 


public class Start extends JFrame implements ActionListener 
{ 
JButton Polski, English; //nazwy przycisków 
JLabel jezyk, language; 
static JLabel wybór; 

public Start() 
{ 
    setSize(330,170);//rozmiar 
    setTitle("MathCalc v0.1 by Majkel"); 
    setLayout(null); 

    jezyk = new JLabel("Choose language:"); 
    jezyk.setBounds(40,10,200,40); 
    add(jezyk); 

    Polski = new JButton("Polski"); 
    Polski.setBounds(40,50,100,30); 
    add(Polski); 
    Polski.addActionListener(this); 

    English = new JButton("English"); 
    English.setBounds(150,50,100,30); 
    add(English); 
    English.addActionListener(this); 

    wybór = new JLabel("Choose"); 
    wybór.setBounds(40,90,400,30); 
    add(wybór); 


} 

public static void main(String[] args) 
{ 
    System.out.println("Choose language:"); 
    Start okno1 = new Start(); 

    okno1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    okno1.setVisible(true); 
} 


@Override 
public void actionPerformed(ActionEvent e) 
{ 
    Object klik = e.getSource(); 
    if(klik==Polski) 
    { 
     wybór.setText("Wybrałeś język Polski.".toString()); //I want to show this massage before exit, but it not show 
     System.out.println("Wybrałeś język Polski."); //This show corect in console 

     try { 
      Thread.sleep(100); 
      this.setVisible(false); 
      new czynnośćPL().setVisible(true); 
     } catch(InterruptedException ex) { 
      Thread.currentThread().interrupt(); 
     } 

    } 

    else if(klik==English) 
    { 
     wybór.setText("You have chosen English.".toString()); //I want to show this massage before exit, but it not show 
     System.out.println("You have chosen English."); //This show corect in console 

     try { 
      Thread.sleep(100); 
      this.setVisible(false); 
      new czynnośćPL().setVisible(true); 
     } catch(InterruptedException ex) { 
      Thread.currentThread().interrupt(); 
     } 

    } 
} 


} 

그리고 두 번째 프로그램 :

Timer timer = new Timer(milliseconds, new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       hideMessageDialog(); 
       Start.this.setVisible(false); 
       new czynnośćPL().setVisible(true); 
      } 
     }); 
     timer.setRepeats(false); 
     timer.start(); 

milliseconds 세트 : 당신은 특별한 지연 후 GUI 스레드에서 작업을 실행하는 데 javax.swing.Timer를 사용할 필요가

import java.awt.Color; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 

public class czynnośćPL extends JFrame implements ActionListener 
{ 
JButton pole, obwód, objętość; //nazwy przycisków 
JLabel Wybór, oblicz; 


public czynnośćPL() 
{ 


    setSize(400,200);//rozmiar 
    setTitle("MathCalc v0.1 by Majkel"); 
    setLayout(null); 
    setBackground(Color.blue); 

    oblicz = new JLabel("Oblicz:"); 
    oblicz.setBounds(40,10,200,40); 
    add(oblicz); 

    pole = new JButton("pole"); 
    pole.setBounds(40,50,100,30); 
    add(pole); 
    pole.addActionListener(this); 

    obwód = new JButton("obwód"); 
    obwód.setBounds(150,50,100,30); 
    add(obwód); 
    obwód.addActionListener(this); 

    objętość = new JButton("objętość"); 
    objętość.setBounds(260,50,100,30); 
    add(objętość); 
    objętość.addActionListener(this); 


} 

public static void main(String[] args) 
{ 
    Start okno1 = new Start(); 
    okno1.getContentPane().setBackground(new Color(189,189,189)); 
    okno1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    okno1.setVisible(true); 


} 


@Override 
public void actionPerformed(ActionEvent e) 
{ 
    Object klik = e.getSource(); 
    if(klik==pole) 
    { 
     System.out.println("Wybrałeś pole."); 

     try { 
      Thread.sleep(100); 
      this.setVisible(false); 
      new polePL().setVisible(true);//co ma open 
     } catch(InterruptedException ex) { 
      Thread.currentThread().interrupt(); 
     } 
    } 

    else if(klik==obwód) 
    { 
     System.out.println("Wybrałeś obwód."); 

     try { 
      Thread.sleep(100); 
      this.setVisible(false); 
      new czynnośćPL().setVisible(true); 
     } catch(InterruptedException ex) { 
      Thread.currentThread().interrupt(); 
     } 
    } 

    else if(klik==objętość) 
    { 
     System.out.println("Wybrałeś objętość."); 

     try { 
      Thread.sleep(100); 
      this.setVisible(false); 
      new czynnośćPL().setVisible(true); 
     } catch(InterruptedException ex) { 
      Thread.currentThread().interrupt(); 
     } 
    } 
} 

} 
+0

죄송 행복 해요 : 전체 Start 클래스입니다. 노력을 제공하고 시도한 것을 보여줘야합니다. – CraigR8806

+1

Welcome to Stack Overflow! [둘러보기] (http://stackoverflow.com/tour)를 방문하고 둘러보고 [도움말 센터] (http://stackoverflow.com/help)를 읽으십시오. 특히 [어떻게 묻는가?] 좋은 질문입니까?] (http://stackoverflow.com/help/how-to-ask) 및 [여기에 대해 내가 들려 줄 주제는 무엇입니까?] (http://stackoverflow.com/help/on-topic). - [Java 명명 규칙] (http://www.oracle.com/technetwork/java/codeconventions-135099.html)을 존중하십시오. 또한 코드를 영어로 번역하십시오. –

+0

JLabel의 새 값을 설정 한 후 revalidate()를 호출 해보십시오. –

답변

0

첫 번째 태스크가 실행되기 전에 지연되고 태스크 간의 지연은 주기적으로 실행됩니다. timer.setRepeats(false);을 호출하면 Timer에게 한 번만 작업을 실행하도록 지시합니다. 작업은 actionPerformed 방법은 ActionListener입니다.

그리고

당신이 할 JFrame의 배경 색상을 변경하려면 :

getContentPane().setBackground(Color.BLUE); 

당신은 다른 주요 방법이했다.

package com.stackoverflow.main; 

import java.awt.Color; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.JButton; 
import javax.swing.JDialog; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 
import javax.swing.Timer; 

public class Start extends JFrame implements ActionListener { 
    JButton Polski, English; // nazwy przycisków 
    JLabel jezyk, language; 
    static JLabel wybór; 
    private JDialog messageDialog; 
    private JLabel messageLabel; 

    public Start() { 
     getContentPane().setBackground(Color.BLUE); 
     setSize(330, 170);// rozmiar 
     setTitle("MathCalc v0.1 by Majkel"); 
     setLayout(null); 

     messageDialog = new JDialog(this); 
     messageLabel = new JLabel("", JLabel.CENTER); 
     messageDialog.setSize(200, 200); 
     messageDialog.getContentPane().add(messageLabel); 

     jezyk = new JLabel("Choose language:"); 
     jezyk.setBounds(40, 10, 200, 40); 
     add(jezyk); 

     Polski = new JButton("Polski"); 
     Polski.setBounds(40, 50, 100, 30); 
     add(Polski); 
     Polski.addActionListener(this); 

     English = new JButton("English"); 
     English.setBounds(150, 50, 100, 30); 
     add(English); 
     English.addActionListener(this); 

     wybór = new JLabel("Choose"); 
     wybór.setBounds(40, 90, 400, 30); 
     add(wybór); 

    } 

    public static void main(String[] args) { 
     System.out.println("Choose language:"); 
     Start okno1 = new Start(); 

     okno1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     okno1.setVisible(true); 
    } 

    @Override 
    public void actionPerformed(ActionEvent e) { 
     Object klik = e.getSource(); 
     if (klik == Polski) { 
      wybór.setText("Wybrałeś język Polski.".toString()); 
      hideThisShowNewFrameAfterDelay(1500); 
      showMessageDialog("Wybrałeś język Polski"); 
      System.out.println("Wybrałeś język Polski."); 
     } 

     else if (klik == English) { 
      wybór.setText("You have chosen English.".toString()); 
      hideThisShowNewFrameAfterDelay(1500); 
      showMessageDialog("You have chosen English."); 
      System.out.println("You have chosen English."); 
     } 
    } 

    private void showMessageDialog(String message) { 
     messageLabel.setText(message); 
     messageDialog.setVisible(true); 
    } 

    private void hideMessageDialog() { 
     messageDialog.dispose(); 
    } 

    private void hideThisShowNewFrameAfterDelay(int milliseconds) { 
     Timer timer = new Timer(milliseconds, new ActionListener() { 

      @Override 
      public void actionPerformed(ActionEvent e) { 
       hideMessageDialog(); 
       Start.this.setVisible(false); 
       new czynnośćPL().setVisible(true); 
      } 
     }); 
     timer.setRepeats(false); 
     timer.start(); 
    } 

} 
+0

그것은 작품 덕분에 :) –

0

덕분에 모두가 지금 노력을 많이 :) 나는 :)이 코딩 서비스가 아니며

import java.awt.Color; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.JLabel; 


public class Start extends JFrame implements ActionListener 
{ 
JButton Polski, English; //nazwy przycisków 
JLabel jezyk, language, wybór; 

public Start() 
{ 
getContentPane().setBackground(new Color (189, 189, 189)); 
setSize(330,170);//rozmiar 
setTitle("MathCalc v0.1 by Majkel"); 
setLayout(null); 

jezyk = new JLabel("Choose language:"); 
jezyk.setBounds(40,10,200,40); 
add(jezyk); 

Polski = new JButton("Polski"); 
Polski.setBounds(40,50,100,30); 
add(Polski); 
Polski.addActionListener(this); 

English = new JButton("English"); 
English.setBounds(150,50,100,30); 
add(English); 
English.addActionListener(this); 

wybór = new JLabel("Choose"); 
wybór.setBounds(40,90,400,30); 
add(wybór); 
} 

public static void main(String[] args) 
{ 
System.out.println("Choose language:"); 
Start okno = new Start(); 
okno.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
okno.setVisible(true); 
} 


@Override 
public void actionPerformed(ActionEvent e) 
{ 
Object klik = e.getSource(); 
if(klik==Polski) 
{ 
    wybór.setText("Wybrałeś język Polski.".toString()); 
    System.out.println("Wybrałeś język Polski."); 
    super.update(this.getGraphics()); 
    try { 
     Thread.sleep(600); 
     this.setVisible(false); 
     new czynnośćPL().setVisible(true); 
    } catch(InterruptedException ex) { 
     Thread.currentThread().interrupt(); 
    } 

} 

else if(klik==English) 
{ 
    wybór.setText("You have chosen English.".toString()); 
    System.out.println("You have chosen English."); 
    super.update(this.getGraphics()); 
    try { 
     Thread.sleep(600); 
     this.setVisible(false); 
     new czynnośćPL().setVisible(true); 
    } catch(InterruptedException ex) { 
     Thread.currentThread().interrupt(); 
    } 

} 
} 

}