图形用户界面中图形顶部的Java按钮
对于我的年终计划,我正在尝试制作一个游戏,帮助一个人研究GUI中显示的不同问题。理想情况下,用户可以按下按钮,查看其答案是否正确。我用变量做了GUI的基础,图形用户界面中图形顶部的Java按钮,java,swing,button,user-interface,graphics,Java,Swing,Button,User Interface,Graphics,对于我的年终计划,我正在尝试制作一个游戏,帮助一个人研究GUI中显示的不同问题。理想情况下,用户可以按下按钮,查看其答案是否正确。我用变量做了GUI的基础,ArrayList变量将包含问题及其答案,并尝试制作按钮 然而,当我试图运行程序时,按钮(我在显示的代码中只有一个)被切断,我无法将它们放在正确的位置。请帮忙 有人请给我看一个经过实际测试并有效的解决方案!我似乎无法根据到目前为止为我发布的内容来获取它 下面是我运行它时的样子: 下面是程序的全部代码: import java.awt.*;
ArrayList
变量将包含问题及其答案,并尝试制作按钮
然而,当我试图运行程序时,按钮(我在显示的代码中只有一个)被切断,我无法将它们放在正确的位置。请帮忙
有人请给我看一个经过实际测试并有效的解决方案!我似乎无法根据到目前为止为我发布的内容来获取它强>
下面是我运行它时的样子:
下面是程序的全部代码:
import java.awt.*;
import javax.swing.*;
import java.awt.geom.Line2D;
import java.util.*;
import java.awt.event.*;
public class EuroGUI extends JPanel {
//Instantiate necessary variables.
ArrayList <String[]> questions = new ArrayList <String[]>(); //Stores (Question + Answers, Correct Answer)
int width = 1280; //GUI Size
int height = 720; // ^
int correct = 0; //Number of correct answers
int attempted = 0; //Number of questions attempted
int streak = 0; //Number of correct answers in a row
int points = 0; //Points accumulated
Font title = new Font("Serif", Font.PLAIN, 60);
Font statsTitle = new Font("Serif", Font.PLAIN, 45);
Font sig = new Font("Mistral", Font.PLAIN, 45);
//Drop down options stuff
JMenu ddMenu = new JMenu("Select an option");
String[] dropDown = new String[] {"A", "B", "C", "D", "E"};
String completion = "starting"; //Determines if the first time repainting
Scanner keyboard = new Scanner(System.in); //Make a keyboard object to test stuff
public static void main(String[]args){ //Main Runner
EuroGUI g = new EuroGUI();
g.setUpScreen();
g.repaint();
}
public void setUpScreen() { //Create the physical GUI, which paints all graphics
//Used http://www.mathcs.emory.edu/~cheung/Courses/377/Syllabus/8-JDBC/GUI/Progs/Layout1.java for buttons
//Create actual GUI window and graphics.
//Create actual GUI window and graphics.
JFrame f = new JFrame("AP European History Study Tool");
JPanel panelGrid = new JPanel();
panelGrid.setLayout(new GridLayout());
setLayout(null);
JPanel panelBorder = new JPanel();
panelBorder.setLayout(new BorderLayout());
JButton xA = new JButton("Choice A");
panelGrid.add(xA, "West");
panelBorder.setLocation(500,500);
f.getContentPane().add(panelBorder);
f.setResizable(false);
f.setVisible(true);
f.setSize(width, height);
f.setBackground(Color.lightGray);
f.add(this);
}
public void paintComponent(Graphics g) { //Draws information on the GUI (Found information on graphics 2D at http://www.tutorialspoint.com/javaexamples/gui_line.htm)
Graphics2D g2 = (Graphics2D) (g);
//Draw a background box which will cover anything that was not re-painted over.
g.setColor(Color.lightGray);
g.fillRect (0, 1280, 0, 720);
//Title "interface"
//Change color back for the lines;
g.setColor(Color.blue);
//Enable bolder lines.
g2.setStroke(new BasicStroke(6));
//Create a box of lines around the title.
g2.draw(new Line2D.Double(200, 0, 200, 120));
g2.draw(new Line2D.Double(200, 120, 1070, 120));
g2.draw(new Line2D.Double(1070, 0, 1070, 120));
g2.draw(new Line2D.Double(200, 0, 1070, 0));
//Fill in box with title with some colors :)
g.setColor(Color.green);
g.fillRect (200, 0, 870, 120);
//Write title
g2.setFont(title);
g.setColor(Color.cyan);
g.drawString("AP European History Study Tool", 240, 80);
g.setColor(Color.black);
g.drawString("AP European History Study Tool", 238, 78);
//Signiature on Title
g.setColor(Color.white);
g2.setFont(sig);
g.drawString("by My Name", 600, 120);
g.setColor(Color.blue);
g.drawString("by My Name", 598, 118);
//Statistics Bar Outline
g.setColor(Color.blue);
g2.draw(new Line2D.Double(1000, 170, 1000, 670));
g2.draw(new Line2D.Double(1000, 170, 1280, 170));
g2.draw(new Line2D.Double(1280, 170, 1280, 670));
g2.draw(new Line2D.Double(1000, 670, 1280, 670));
g2.setStroke(new BasicStroke(6));
g.setColor(Color.black);
g.fillRect (1000, 170, 1280, 500);
g.setColor(Color.green); //Underline
g2.setStroke(new BasicStroke(2));
g2.draw(new Line2D.Double(1055, 230, 1215, 230));
g2.setStroke(new BasicStroke(6));
//Overall Score
g2.setFont(statsTitle);
g2.setColor(Color.green);
g.drawString("Statistics", 1055, 220);
g2.setColor(Color.cyan);
g.drawString(correct + "/" + attempted + " Correct", 1035, 285);
//Streak
if (streak >= 3)
{
g2.setColor(Color.red);
g.drawString(streak + " Streak", 1060, 340);
}
else{
g2.setColor(Color.cyan);
g.drawString(streak + " Streak", 1060, 340);
}
if (completion.equals("starting")){
}
}
}
import java.awt.*;
导入javax.swing.*;
导入java.awt.geom.Line2D;
导入java.util.*;
导入java.awt.event.*;
公共类EuroGUI扩展JPanel{
//实例化必要的变量。
ArrayList questions=new ArrayList();//存储(问题+答案,正确答案)
int width=1280;//GUI大小
内部高度=720;//^
int correct=0;//正确答案的数目
int尝试=0;//尝试的问题数
int streak=0;//一行中正确答案的数目
int points=0;//累计点数
字体标题=新字体(“衬线”,Font.PLAIN,60);
Font statsTitle=新字体(“衬线”,Font.PLAIN,45);
Font sig=新字体(“Mistral”,Font.PLAIN,45);
//下拉选项
JMenu ddMenu=新建JMenu(“选择选项”);
字符串[]下拉列表=新字符串[]{“A”、“B”、“C”、“D”、“E”};
String completion=“start”;//确定是否第一次重新绘制
扫描器键盘=新扫描器(System.in);//制作一个键盘对象来测试东西
公共静态void main(字符串[]args){//main Runner
EuroGUI g=新EuroGUI();
g、 设置屏幕();
g、 重新油漆();
}
public void setUpScreen(){//创建绘制所有图形的物理GUI
//使用http://www.mathcs.emory.edu/~cheung/Courses/377/提纲/8-JDBC/GUI/Progs/Layout1.java用于按钮
//创建实际的GUI窗口和图形。
//创建实际的GUI窗口和图形。
JFrame f=新JFrame(“AP欧洲历史研究工具”);
JPanel panelGrid=新的JPanel();
setLayout(新的GridLayout());
setLayout(空);
JPanel panelBorder=新的JPanel();
setLayout(新的BorderLayout());
JButton xA=新JButton(“选择A”);
panelGrid.添加(xA,“西部”);
面板边框设置位置(500500);
f、 getContentPane().add(panelBorder);
f、 可设置大小(假);
f、 setVisible(真);
f、 设置尺寸(宽度、高度);
f、 挫折背景(颜色:浅灰色);
f、 加上(这个);
}
public void paintComponent(Graphics g){//在GUI上绘制信息http://www.tutorialspoint.com/javaexamples/gui_line.htm)
图形2d g2=(图形2d)(g);
//画一个背景框,该框将覆盖所有未重新绘制的内容。
g、 setColor(颜色:浅灰色);
g、 fillRect(01280,0720);
//标题“接口”
//改变线的颜色;
g、 setColor(Color.blue);
//启用更粗的行。
g2.设定行程(新基本行程(6));
//围绕标题创建一个线框。
g2.绘制(新线条2D.双(200,0,200,120));
g2.绘制(新线条2D.双(2001201070120));
g2.绘制(新线条2D.双(1070,01070120));
g2.绘制(新线条2D.双色(200,0,1070,0));
//用一些颜色填写标题框:)
g、 setColor(Color.green);
g、 fillRect(200,0870120);
//书名
g2.设置字体(标题);
g、 setColor(Color.cyan);
g、 抽绳(“AP欧洲历史研究工具”,240,80);
g、 设置颜色(颜色为黑色);
g、 抽绳(“AP欧洲历史研究工具”,23878);
//标题签名
g、 setColor(Color.white);
g2.setFont(sig);
g、 抽绳(“我的名字”,600,120);
g、 setColor(Color.blue);
g、 抽绳(“我的名字”,598118);
//统计栏概要
g、 setColor(Color.blue);
g2.绘制(新线条2D.双(10001701000670));
g2.绘制(新线条2D.双色(10001701280170));
g2.绘制(新线条2D.双(12801701280670));
g2.绘制(新线条2D.双色(10006701280670));
g2.设定行程(新基本行程(6));
g、 设置颜色(颜色为黑色);
g、 fillRect(10001701280500);
g、 setColor(Color.green);//下划线
g2.设定行程(新基本行程(2));
g2.绘制(新线条2D.双(105523021215230));
g2.设定行程(新基本行程(6));
//总分
g2.设置字体(statsTitle);
g2.设置颜色(颜色为绿色);
g、 抽绳(“统计”,1055220);
g2.setColor(颜色为青色);
g、 抽绳(正确+“/”+尝试+“正确”,1035285);
//条纹
如果(条纹>=3)
{
g2.设置颜色(颜色为红色);
g、 抽绳(条纹+条纹),1060340;
}
否则{
g2.setColor(颜色为青色);
g、 抽绳(条纹+条纹),1060340;
}
if(完成等于(“开始”)){
}
}
}
您没有正确设置布局。可能不是您想要的,但如果您从FlowLayout更改为BorderLayout,似乎可以解决您的问题。此外,在代码中的任何地方都不使用panel2,因此可以删除它
public void setUpScreen() { //Create the physical GUI, which paints all graphics
//Used http://www.mathcs.emory.edu/~cheung/Courses/377/Syllabus/8-JDBC/GUI/Progs/Layout1.java for buttons
//Create actual GUI window and graphics.
JFrame f = new JFrame("AP European History Study Tool");
JPanel panelGrid = new JPanel();
panelGrid.setLayout(new GridLayout());
setLayout(null);
JPanel panelBorder = new JPanel();
panelBorder.setLayout(new BorderLayout());
JButton xA = new JButton("Choice A");
panelGrid.add(xA, "West");
panelBorder.setLocation(500,500);
f.getContentPane().add(panelBorder);
f.setResizable(false);
f.setVisible(true);
f.setSize(width, height);
f.setBackground(Color.lightGray);
f.add(this);
}
这是一个交响乐
public void paintComponent(Graphics g) { //Draws information on the GUI (Found information on graphics 2D at http://www.tutorialspoint.com/javaexamples/gui_line.htm)
Graphics2D g2 = (Graphics2D) (g);
public void paintComponent(Graphics g) { //Draws information on the GUI (Found information on graphics 2D at http://www.tutorialspoint.com/javaexamples/gui_line.htm)
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) (g);
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import javax.swing.border.MatteBorder;
public class Example {
public static void main(String[] args) {
new Example();
}
public Example() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new HeaderPane(), BorderLayout.NORTH);
frame.add(new StatisticsPane(), BorderLayout.EAST);
frame.add(new QuestionPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class HeaderPane extends JPanel {
public HeaderPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 0;
gbc.anchor = GridBagConstraints.SOUTH;
// gbc.ipadx = 100;
NamePane namePane = new NamePane();
FontMetrics fm = namePane.getFontMetrics(namePane.getFont());
add(namePane, gbc);
gbc.insets = new Insets(0, 0, fm.getDescent(), 0);
gbc.gridx = 0;
gbc.gridwidth = 2;
gbc.ipadx = 10;
gbc.ipady = 10;
gbc.anchor = GridBagConstraints.CENTER;
add(new TitlePane(), gbc);
}
public class ShadowLabel extends JPanel {
private String text;
private Color shadowColor;
private int shadowOffset;
public ShadowLabel(String text, Color shadowColor) {
this.text = text;
this.shadowColor = shadowColor;
this.shadowOffset = 2;
}
public int getShadowOffset() {
return shadowOffset;
}
public void setShadowOffset(int shadowOffset) {
this.shadowOffset = shadowOffset;
}
@Override
public Dimension getPreferredSize() {
FontMetrics fm = getFontMetrics(getFont());
return new Dimension(fm.stringWidth(getText()), fm.getHeight());
}
public String getText() {
return text;
}
public Color getShadowColor() {
return shadowColor;
}
public void setText(String text) {
this.text = text;
repaint();
}
public void setShadowColor(Color shadowColor) {
this.shadowColor = shadowColor;
repaint();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setFont(getFont());
FontMetrics fm = g.getFontMetrics();
int x = (getWidth() - fm.stringWidth(getText())) / 2;
int y = (getHeight() - fm.getHeight()) / 2;
g.setColor(getShadowColor());
g.drawString(getText(), x + getShadowOffset(), y + getShadowOffset() + fm.getAscent());
g.setColor(getForeground());
g.drawString(getText(), x, y + fm.getAscent());
}
}
public class TitlePane extends ShadowLabel {
public TitlePane() {
super("AP European History Study Tool", Color.CYAN);
setBackground(Color.GREEN);
setBorder(new MatteBorder(0, 1, 1, 1, Color.BLUE));
setFont(new Font("Serif", Font.PLAIN, 60));
}
}
public class NamePane extends ShadowLabel {
public NamePane() {
super("by Me", Color.WHITE);
setForeground(Color.BLUE);
setFont(new Font("Mistral", Font.PLAIN, 45));
setOpaque(false);
}
}
}
public class StatisticsPane extends JPanel {
private JLabel score;
private JLabel streak;
public StatisticsPane() {
setLayout(new BorderLayout());
setBackground(Color.BLACK);
setBorder(new CompoundBorder(new LineBorder(Color.BLUE), new EmptyBorder(4, 4, 4, 4)));
JLabel statistics = new JLabel("Statistics");
statistics.setFont(new Font("Serif", Font.PLAIN, 45));
statistics.setForeground(Color.GREEN);
statistics.setBorder(new CompoundBorder(new MatteBorder(0, 0, 1, 0, Color.GREEN), new EmptyBorder(4, 4, 4, 4)));
add(statistics, BorderLayout.NORTH);
score = new JLabel("0/0 correct");
score.setForeground(Color.GREEN);
score.setFont(new Font("Serif", Font.PLAIN, 45));
streak = new JLabel("0 streak");
streak.setForeground(Color.GREEN);
streak.setFont(new Font("Serif", Font.PLAIN, 45));
JPanel pnl = new JPanel(new GridBagLayout());
pnl.setOpaque(false);
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
pnl.add(score, gbc);
gbc.gridy++;
gbc.weighty = 1;
gbc.anchor = GridBagConstraints.NORTH;
pnl.add(streak, gbc);
add(pnl);
}
}
public class QuestionPane extends JPanel {
public QuestionPane() {
setLayout(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridwidth = GridBagConstraints.REMAINDER;
gbc.anchor = GridBagConstraints.WEST;
JButton xA = new JButton("Choice A");
add(xA, gbc);
}
}
}