2013-03-17 3 views
0

Итак, моя программа предназначена для принятия двух значений, вычисления и вычисления этого значения. Я хочу отобразить его в виде приглашения в графическом интерфейсе в секции actionPerformed после нажатия кнопки. Похоже, он должен появиться, но я не могу найти, почему это не так? Это «подсказка2», которая не появляется. БлагодаряJava GUI не отображает приглашение, я хочу, чтобы оно было

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
import java.util.*; 
public class Windchill extends JFrame implements ActionListener{ 
    private static final int FRAME_WIDTH = 300; 
    private static final int FRAME_HEIGHT = 200; 
    private static final int FRAME_X_ORIGIN = 150; 
    private static final int FRAME_Y_ORIGIN = 250; 
    private String degree; 
    private String wind; 
    private int degreeInt; 
    private int windInt; 
    private double windChillInt; 
    private JButton windButton; 
    private JLabel prompt; 
    private JLabel prompt1; 
    private JLabel prompt2; 
    private JTextField inputLine; 
    private JTextField inputLine1; 

    public Windchill(){ 
     setTitle("Windchill"); 
     setSize(FRAME_WIDTH, FRAME_HEIGHT); 
     setLocation(FRAME_X_ORIGIN, FRAME_Y_ORIGIN); 
     setDefaultCloseOperation(EXIT_ON_CLOSE); 
     Container contentPane = getContentPane(); 
     contentPane.setLayout(new FlowLayout());  
     inputLine = new JTextField(); 
     inputLine.setColumns(3); 
     inputLine.addActionListener(this); 
     contentPane.add(inputLine); 
     prompt = new JLabel(); 
     prompt.setText("Enter the degrees in Farienheight  "); 
     prompt.setSize(150,25); 
     contentPane.add(prompt); 
     inputLine1 = new JTextField(); 
     inputLine1.setColumns(3); 
     inputLine1.addActionListener(this); 
     contentPane.add(inputLine1); 

     prompt1 = new JLabel(); 
     prompt1.setText("Enter the wind speed in MPH"); 
     prompt1.setSize(150,25); 
     contentPane.add(prompt1); 

     windButton = new JButton("Calculate windchill"); 
     contentPane.add(windButton); 
     windButton.addActionListener(this); 


    } 
    public void actionPerformed(ActionEvent event){ 
     if (event.getSource() instanceof JButton){ 
      JButton clickedButton = (JButton) event.getSource(); 
      if (clickedButton == windButton){ 
       degree = inputLine.getText(); 
       degreeInt = Integer.parseInt(degree); 
       wind = inputLine1.getText(); 
       windInt = Integer.parseInt(wind); 
       windChillInt = 0.08 * (degreeInt - 91.4)*(3.71* (Math.sqrt(windInt)) + 5.81 - 0.25 *windInt) + 91.4; 

       prompt2 = new JLabel(); 
       prompt2.setText("The windchill is " + windChillInt); 
       prompt2.setSize(150,25); 
       Container contentPane = getContentPane(); 
       contentPane.setLayout(new FlowLayout()); 
       contentPane.add(prompt2); 
      } 

     } 
    } 
    public static void main(String[] args) { 
     Windchill frame; 
     frame = new Windchill(); 
     frame.setVisible(true); 
    } 
} 

ответ

1

prompt2 не отображается, как должны быть проверены для любых новых добавленных компонентов Swing контейнеры могут быть видны. Также это хорошая практика: repaint. Вы можете сделать:

contentPane.revalidate(); 
contentPane.repaint(); 

Боковые Примечания:

  • Излишне установить макет снова для вашей ActionListener
  • contentPane.add(prompt2) может быть упрощена add(prompt2)
  • Альтернативно,, вы могли бы просто dd prompt2JLabelcontentPane при запуске с пустым String содержанием и звоните setText для обновления.
Смежные вопросы