2014-11-14 73 views
0

我一直在研究这个程序,并且我很难在弹出的窗口中显示结果。当程序运行时,我在控制台中显示正确的输出,但是当选择单选按钮时,我需要它显示在单选按钮下的窗口中。在此先感谢,任何帮助,非常感谢。如何将列表字符串传递给JLabel并显示在JFrame中

import TrySource.TryWindow; 
 

 
import java.awt.BorderLayout; 
 
import java.awt.Color; 
 

 
import java.awt.event.ItemEvent; 
 
import java.awt.event.ItemListener; 
 
import java.util.Arrays; 
 
import java.util.Random; 
 
import javax.swing.*; 
 

 

 
public class TrySomethingNew extends JFrame 
 
{ 
 

 
    // Radio buttons for selecting colors 
 
    private JRadioButton jrbMexican, jrbItalian; 
 
     JLabel jlblResult; 
 

 
    // Declare a panel for displaying message 
 
    private TryWindow TryWindow; 
 

 
    // Main method 
 
    public static void main(String[] args) 
 
    { 
 
    TrySomethingNew frame = new TrySomethingNew(); 
 
    frame.pack(); 
 
    frame.setSize(500,500); 
 
    frame.setTitle("Try Something New"); 
 
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
 
    frame.setLocationRelativeTo(null); // Center the frame 
 
    frame.setVisible(true); 
 
} 
 

 
    public TrySomethingNew() 
 
    { 
 
    // Create a MovingMessageCanvas instance and set colors 
 
    TryWindow = new TryWindow("Let's try this."); 
 
    TryWindow.setBackground(Color.WHITE); 
 

 
    // Panel to hold radio buttons 
 
    JPanel jpRadioButtons = new JPanel(); 
 
    jpRadioButtons.setBorder(new javax.swing.border.TitledBorder("Select A Food Genre")); 
 
    jpRadioButtons.add(jrbMexican = new JRadioButton("Mexican")); 
 
    jpRadioButtons.add(jrbItalian = new JRadioButton("Italian")); 
 

 

 
    // Group radio buttons 
 
    ButtonGroup btg = new ButtonGroup(); 
 
    btg.add(jrbMexican); 
 
    btg.add(jrbItalian); 
 
    
 
    //Panel to hold result 
 
    JPanel jpResultPanel = new JPanel(); 
 
    jpResultPanel.setBorder(new javax.swing.border.TitledBorder("Result"));  
 

 
    // Place panels in the frame 
 
    setLayout(new BorderLayout()); 
 
    add(TryWindow, BorderLayout.CENTER); 
 
    add(jpRadioButtons, BorderLayout.NORTH); 
 
    add(jpResultPanel, BorderLayout.CENTER); 
 
    
 

 
    // Register listeners with the buttons 
 

 
    jrbMexican.addItemListener(new EventListener()); 
 
    jrbItalian.addItemListener(new EventListener()); 
 
    
 
    
 
    }//end main 
 

 
    // Handle radio button selections 
 
    class EventListener implements ItemListener 
 
    { 
 
     
 
    public void itemStateChanged(ItemEvent e) 
 
    { 
 
     if (jrbMexican.isSelected()) 
 
     { 
 
     java.util.List<String> mexicanList = Arrays.asList("Jose Locos\n853 N Glenstone Ave, Springfield, MO 65802\n(417) 831-1300", 
 
     "Amigos Mexican Restaurant\n2118 S Campbell Ave, Springfield, MO 65807\n(417) 887-1401"); 
 
     
 
     Random randomizer = new Random(); 
 
     String random = mexicanList.get(randomizer.nextInt(mexicanList.size())); 
 
     System.out.println(random); 
 
     
 
     
 
     }//end if jrbMexican isSelected 
 
     
 
     if (jrbItalian.isSelected()) 
 
     { 
 
     java.util.List<String> italianList = Arrays.asList("Zios Italian Kitchen\n1249 E Kingsley St, Springfield, MO 65804\n(417) 889-1919", 
 
     "Bambinos Cafe\n1141 E Delmar St, Springfield, MO 65807\n(417) 862-9999"); 
 
     
 
     Random randomizer = new Random(); 
 
     String random = italianList.get(randomizer.nextInt(italianList.size())); 
 
     System.out.println(random); 
 
     
 
     }//end if jrbItalian isSelected 
 
    }//end itemStateChanged 
 
    }//end eventListener 
 
}//end TrySomethingNew 
 

 

package TrySource; 
 

 
// TryWindow.java: Display a message on a JPanel 
 
import java.awt.FontMetrics; 
 
import java.awt.Graphics; 
 
import javax.swing.JPanel; 
 

 
public class TryWindow extends JPanel 
 
{ 
 
    /** The message to be displayed */ 
 
    private String message = "Try Something New"; 
 

 
    /** The x coordinate where the message is displayed */ 
 
    private int xCoordinate = 465; 
 

 
    /** The y coordinate where the message is displayed */ 
 
    private int yCoordinate = 40; 
 

 
    /** Indicate whether the message is displayed in the center */ 
 
    private boolean centered; 
 

 
    /** The interval for moving the message horizontally and vertically */ 
 
    private int interval = 10; 
 

 
    /** Default constructor */ 
 
    public TryWindow() 
 
    { 
 
     
 
    } 
 

 
    /** Constructor with a message parameter */ 
 
    public TryWindow(String message) 
 
    { 
 
    this.message = message; 
 
    } 
 

 
    /** Return message */ 
 
    public String getMessage() 
 
    { 
 
    return message; 
 
    } 
 

 
    /** Set a new message */ 
 
    public void setMessage(String message) 
 
    { 
 
    this.message = message; 
 
    repaint(); 
 
    } 
 

 
    /** Return xCoordinator */ 
 
    public int getXCoordinate() 
 
    { 
 
    return xCoordinate; 
 
    } 
 

 
    /** Set a new xCoordinator */ 
 
    public void setXCoordinate(int x) 
 
    { 
 
    this.xCoordinate = x; 
 
    repaint(); 
 
    } 
 

 
    /** Return yCoordinator */ 
 
    public int getYCoordinate() 
 
    { 
 
    return yCoordinate; 
 
    } 
 

 
    /** Set a new yCoordinator */ 
 
    public void setYCoordinate(int y) 
 
    { 
 
    this.yCoordinate = y; 
 
    repaint(); 
 
    } 
 

 
    /** Return centered */ 
 
    public boolean isCentered() 
 
    { 
 
    return centered; 
 
    } 
 

 
    /** Set a new centered */ 
 
    public void setCentered(boolean centered) 
 
    { 
 
    this.centered = centered; 
 
    repaint(); 
 
    } 
 

 
    /** Return interval */ 
 
    public int getInterval() 
 
    { 
 
    return interval; 
 
    } 
 

 
    /** Set a new interval */ 
 
    public void setInterval(int interval) 
 
    { 
 
    this.interval = interval; 
 
    repaint(); 
 
    } 
 

 
    @Override 
 
    protected void paintComponent(Graphics g) 
 
    { 
 
    super.paintComponent(g); 
 

 
    if (centered) 
 
    { 
 
     // Get font metrics for the current font 
 
     FontMetrics fm = g.getFontMetrics(); 
 

 
     // Find the center location to display 
 
     int stringWidth = fm.stringWidth(message); 
 
     int stringAscent = fm.getAscent(); 
 
     // Get the position of the leftmost character in the baseline 
 
     xCoordinate = getWidth()/2 - stringWidth/2; 
 
     yCoordinate = getHeight()/2 + stringAscent/2; 
 
    } 
 

 
    g.drawString(message, xCoordinate, yCoordinate); 
 
    } 
 

 
    }

+0

'添加(TryWindow,BorderLayout.CENTER);'应该被去掉,jlblResult应该得到足够大的优选的和最小的尺寸和添加到结果面板用BorderLayout的太在中心。 – 2014-11-14 15:36:41

回答

0
private TextArea textArea1; 

// declear变量

下,在这方面您需要的信息将其添加到帧不是显示在这方面你的结果
textArea1 = new TextArea(); 

//把它的构造

jpResultPanel.add(textArea1); 
textArea1.setVisible(true); 

把这些行之后 //注册听众的按钮 AND

textArea1.setText(random); 

把它放在你打印结果的地方。

+0

非常感谢你的帮助! – Butters573 2014-11-15 04:37:52

0

你需要做出这样的TextArea textArea1的textarea的=新的TextArea(); 并通过调用它的setText方法,这样就可以显示结果