当我选择一个单选按钮时,我的程序会显示一个随机的餐厅。我现在要做的是在选择单选按钮时显示的文本下方显示餐馆标志的图像。我真的不确定我需要做什么,或者我需要在哪里寻找有关如何实现这一目标的信息。提前感谢您的时间和帮助。
import TrySource.TryWindow;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.JLabel;
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;
//textarea for result
private JLabel jlblRestaurantName;
// 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(jpRadioButtons, BorderLayout.NORTH);
add(jpResultPanel, BorderLayout.CENTER);
// Register listeners with the buttons
jrbMexican.addItemListener(new EventListener());
jrbItalian.addItemListener(new EventListener());
jlblRestaurantName = new JLabel();
jpResultPanel.add(jlblRestaurantName);
jlblRestaurantName.setVisible(true);
}//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(
"<html>Jose Locos<br>853 N Glenstone Ave, Springfield, MO 65802<br>(417) 831-1300<html>",
"<html>Amigos Mexican Restaurant<br>2118 S Campbell Ave, Springfield, MO 65807<br>(417) 887-1401<html>");
Random randomizer = new Random();
String random = mexicanList.get(randomizer.nextInt(mexicanList.size()));
jlblRestaurantName.setText(random);
}//end if jrbMexican isSelected
if (jrbItalian.isSelected())
{
java.util.List<String> italianList = Arrays.asList(
"<html>Zios Italian Kitchen<br>1249 E Kingsley St, Springfield, MO 65804<br>(417) 889-1919<html>",
"<html>Bambinos Cafe<br>1141 E Delmar St, Springfield, MO 65807<br>(417) 862-9999<html>");
Random randomizer = new Random();
String random = italianList.get(randomizer.nextInt(italianList.size()));
jlblRestaurantName.setText(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 :(得分:0)
首先,您创建一个模型类来保存有关餐馆的信息。
package com.ggl.testing;
import java.awt.Image;
public class Restaurant {
private final Image image;
private final RestaurantType restaurantType;
private final String name;
private final String address;
private final String phoneNumber;
public Restaurant(Image image, RestaurantType restaurantType, String name,
String address, String phoneNumber) {
this.image = image;
this.restaurantType = restaurantType;
this.name = name;
this.address = address;
this.phoneNumber = phoneNumber;
}
public Image getImage() {
return image;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
public String getPhoneNumber() {
return phoneNumber;
}
public RestaurantType getRestaurantType() {
return restaurantType;
}
public enum RestaurantType {
MEXICAN, ITALIAN
}
}
接下来,您修改代码以使用Restaurant类。
定义餐馆列表。
private List<Restaurant> restaurants = new ArrayList<>();
接下来,添加您的餐馆信息。
Restaurant restaurant1 = new Restaurant(image, RestaurantType.MEXICAN,
"Jose Locos", "853 N Glenstone Ave, Springfield, MO 65802",
"(417) 831-1300");
restaurants.add(restaurant1);
...
最后,修改itemStateChanged方法以使用餐厅列表。
您必须从Java项目中读取图像。这是实现这一目标的一种方法。
private Image getImage(String fileName) {
try {
return ImageIO.read(getClass().getResourceAsStream(fileName));
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
将图像放在与Java源代码相同的目录中。这样,您将文件的名称传递给getImage方法。不需要路径字符串。
在构建GUI之前阅读所有图像。您只需要一次读取图像。