我怎样才能让我的 JButtons 进入与我的图像相同的 window?
How can I get my JButtons to go into the same window as my image?
我正在尝试编写一个二十一点游戏,我想要一个 window,其中既有 table 图像,又有 hit/stay 按钮。但是,即使我尝试将 hit/stay 按钮对象添加(@param)到框架中,按钮也会单独显示 windows 作为 table.
我的代码:
import java.awt.*;
import javax.swing.*;
public class BlackjackTable extends JComponent{
private static final int WIDTH = 1200;
private static final int HEIGHT = 800;
private Rectangle table;
private JButton hitOrStay;
public BlackjackTable(){
table = new Rectangle(0,0,WIDTH,HEIGHT);
JFrame frame = new JFrame();
JLabel lab = new JLabel(new ImageIcon("blackjackTableCanvas.jpg"));
frame.setSize(1200,800);
lab.setSize(1200,800);
frame.add(lab);
hitOrStay = new HitOrStayButton();
frame.add(hitOrStay);
frame.setTitle("Test Canvas");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(this);
frame.setVisible(true);
}
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;
g2.draw(table);
}
public static void main(String[] args){
BlackjackTable b = new BlackjackTable();
}
}
我的点击或停留按钮:
public class HitOrStayButton extends JButton{
JButton stayButton = new JButton("STAY");
JButton hitButton = new JButton("HIT");
public HitOrStayButton(){
ActionListener pressChoice = new DecisionListener();
hitButton.addActionListener(pressChoice);
stayButton.addActionListener(pressChoice);
JPanel testPanel = new JPanel();
testPanel.add(hitButton);
testPanel.add(stayButton);
JFrame testFrame = new JFrame();
testFrame.add(testPanel);
testFrame.setSize(300, 150);
testFrame.setVisible(true);
}
class DecisionListener implements ActionListener{
public void actionPerformed(ActionEvent a){
if(a.getSource() == hitButton){
System.out.println("YOU CHOSE HIT!");
}
else if(a.getSource() == stayButton){
System.out.println("YOU CHOSE STAY!");
}
}
}
public static void main(String[] args){
HitOrStayButton h = new HitOrStayButton();
}
}
如何让框架在底部的面板中包含图像和按钮?
public class HitOrStayButton extends JButton{
JButton stayButton = new JButton("STAY");
JButton hitButton = new JButton("HIT");
你认为一辆车里面还有两辆车吗?
首先通过tutorials。
in which both the table image sits, and the hit/stay buttons sits
您需要学会有效地使用 "layout managers"。框架内容窗格的默认布局管理器是 BorderLayout
。您不能只将多个组件添加到同一位置。添加组件时,您需要指定不同的约束条件(如 BorderLayout.CENTER
和 BorderLayout.PAGE_START
)。
阅读 How to Use BorderLayout 上的 Swing 教程部分,了解更多信息和工作示例以帮助您入门。
我正在尝试编写一个二十一点游戏,我想要一个 window,其中既有 table 图像,又有 hit/stay 按钮。但是,即使我尝试将 hit/stay 按钮对象添加(@param)到框架中,按钮也会单独显示 windows 作为 table.
我的代码:
import java.awt.*;
import javax.swing.*;
public class BlackjackTable extends JComponent{
private static final int WIDTH = 1200;
private static final int HEIGHT = 800;
private Rectangle table;
private JButton hitOrStay;
public BlackjackTable(){
table = new Rectangle(0,0,WIDTH,HEIGHT);
JFrame frame = new JFrame();
JLabel lab = new JLabel(new ImageIcon("blackjackTableCanvas.jpg"));
frame.setSize(1200,800);
lab.setSize(1200,800);
frame.add(lab);
hitOrStay = new HitOrStayButton();
frame.add(hitOrStay);
frame.setTitle("Test Canvas");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(this);
frame.setVisible(true);
}
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;
g2.draw(table);
}
public static void main(String[] args){
BlackjackTable b = new BlackjackTable();
}
}
我的点击或停留按钮:
public class HitOrStayButton extends JButton{
JButton stayButton = new JButton("STAY");
JButton hitButton = new JButton("HIT");
public HitOrStayButton(){
ActionListener pressChoice = new DecisionListener();
hitButton.addActionListener(pressChoice);
stayButton.addActionListener(pressChoice);
JPanel testPanel = new JPanel();
testPanel.add(hitButton);
testPanel.add(stayButton);
JFrame testFrame = new JFrame();
testFrame.add(testPanel);
testFrame.setSize(300, 150);
testFrame.setVisible(true);
}
class DecisionListener implements ActionListener{
public void actionPerformed(ActionEvent a){
if(a.getSource() == hitButton){
System.out.println("YOU CHOSE HIT!");
}
else if(a.getSource() == stayButton){
System.out.println("YOU CHOSE STAY!");
}
}
}
public static void main(String[] args){
HitOrStayButton h = new HitOrStayButton();
}
}
如何让框架在底部的面板中包含图像和按钮?
public class HitOrStayButton extends JButton{ JButton stayButton = new JButton("STAY"); JButton hitButton = new JButton("HIT");
你认为一辆车里面还有两辆车吗?
首先通过tutorials。
in which both the table image sits, and the hit/stay buttons sits
您需要学会有效地使用 "layout managers"。框架内容窗格的默认布局管理器是 BorderLayout
。您不能只将多个组件添加到同一位置。添加组件时,您需要指定不同的约束条件(如 BorderLayout.CENTER
和 BorderLayout.PAGE_START
)。
阅读 How to Use BorderLayout 上的 Swing 教程部分,了解更多信息和工作示例以帮助您入门。