我正在创建一个简单的游戏,涉及玩按钮。我的代码分为两个独立的程序,GraphicsMain 和 GraphicsPanel。总的来说,我已经构建了 Jframe 和按钮。在面板中,是我的图形。我的问题是尝试让 PaintComponent 和图形在我的主程序中工作。
我尝试将库和代码放入第一个程序(主程序)中,但它不起作用......
//Here is my main program:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;
public class GraphicsMain extends JFrame{
public static void main(String[] args){
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;
GraphicsMain window = new GraphicsMain();
JPanel p = new JPanel();
p.add(new GraphicsPanel()); // add a class that extends JPanel
window.setTitle("Arrays Project");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setContentPane(p);
window.pack();
window.setLocationRelativeTo(null);
window.setVisible(true);
//Buttons for Players to select their move:
JButton button = new JButton("Add 1:");
button.setBounds(29, 400, 75, 50);
JButton button2 = new JButton("Add 2:");
button2.setBounds(101, 400, 75, 50);
JButton button3 = new JButton("Add 1:");
button3.setBounds(329, 400, 75, 50);
JButton button4 = new JButton("Add 2:");
button4.setBounds(401, 400, 75, 50);
window.setLayout(null);
//Adds buttons to the frame:
window.add(button);
window.add(button2);
window.add(button3);
window.add(button4);
//Makes the frame visible:
window.setVisible(true);
//Action that occurs when the players click on their buttons:
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
g2.fillOval(20, 20, 20, 20);
}
});
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Display a message dialog when the button is clicked
JOptionPane.showMessageDialog(window, "Button Clicked!");
}
});
button3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Display a message dialog when the button is clicked
JOptionPane.showMessageDialog(window, "Button Clicked!");
}
});
button4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// Display a message dialog when the button is clicked
JOptionPane.showMessageDialog(window, "Button Clicked!");
}
});
}
}
}
//Here is my panel program:
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
import java.awt.Font;
public class GraphicsPanel extends JPanel{
public GraphicsPanel(){
setPreferredSize(new Dimension(500, 500));
}
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D) g;
//1x10 board for the game:
int count = 0;
int [] board = new int [10];
for (int i = 0; i < board.length; i++) {
g2.drawRect(0 + count, 250, 50, 50);
count += 50;
}
//Labels for each square 1-10:
g2.setFont(new Font("TimesRoman", Font.PLAIN, 20));
g2.drawString("1", 20, 325);
g2.drawString("2", 70, 325);
g2.drawString("3", 120, 325);
g2.drawString("4", 170, 325);
g2.drawString("5", 220, 325);
g2.drawString("6", 270, 325);
g2.drawString("7", 320, 325);
g2.drawString("8", 370, 325);
g2.drawString("9", 420, 325);
g2.drawString("10", 465, 325);
//Title and instructions for players:
g2.drawString("1, 2, …, 10 Game", 175, 25);
g2.drawString("To Play: Players alternate placing either one or two pieces", 10, 60);
g2.drawString("on the leftmost open squares. In this game, we don’t", 30, 85);
g2.drawString("distinguish between players’ pieces, so we’ll call", 40, 110);
g2.drawString("them left and right. Left will go first.", 85, 135);
g2.drawString("The first player to place the tenth piece on the board wins!", 15, 170);
//Labels for players buttons:
g2.drawString("Player 1", 67, 385);
g2.drawString("Player 2", 367, 385);
}
}
我建议您进行一些更改,首先声明一个静态 GraphicsPanel 对象,然后在 main 方法中实例化该对象,这需要能够调用我们在 GraphicsPanel 类中创建的 setDrawing 方法,以便启用相关画画。
在图形主中:
// delete public void paintComponent(Graphics g){
// delete Graphics2D g2 = (Graphics2D) g;
pan = new GraphicsPanel();
p.add( pan ); // add a class that extends JPanel
button.addActionListener( new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
pan.setDrawing( true );
pan.repaint();
}
});
在图形面板中:
boolean drawing;
public void setDrawing( boolean draw ) {
dibujar = draw;
}
在paintComponent中:
if( dibujar ) {
g2.fillOval( 20, 20, 20, 20 );
}
所有代码:
public class GraphicsMain extends JFrame {
static GraphicsPanel pan;
public static void main( String[] args ) {
GraphicsMain window = new GraphicsMain();
JPanel p = new JPanel();
pan = new GraphicsPanel();
p.add( pan ); // add a class that extends JPanel
window.setTitle( "Arrays Project" );
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
window.setContentPane( p );
window.pack();
window.setLocationRelativeTo( null );
window.setVisible( true );
//Buttons for Players to select their move:
JButton button = new JButton( "Add 1:" );
button.setBounds( 29, 400, 75, 50 );
JButton button2 = new JButton( "Add 2:" );
button2.setBounds( 101, 400, 75, 50 );
JButton button3 = new JButton( "Add 1:" );
button3.setBounds( 329, 400, 75, 50 );
JButton button4 = new JButton( "Add 2:" );
button4.setBounds( 401, 400, 75, 50 );
window.setLayout( null );
//Adds buttons to the frame:
window.add( button );
window.add( button2 );
window.add( button3 );
window.add( button4 );
//Makes the frame visible:
window.setVisible( true );
//Action that occurs when the players click on their buttons:
button.addActionListener( new ActionListener() {
@Override
public void actionPerformed( ActionEvent e ) {
pan.setDrawing( true );
pan.repaint();
}
} );
button2.addActionListener( new ActionListener() {
@Override
public void actionPerformed( ActionEvent e ) {
// Display a message dialog when the button is clicked
JOptionPane.showMessageDialog( window, "Button Clicked!" );
}
} );
button3.addActionListener( new ActionListener() {
@Override
public void actionPerformed( ActionEvent e ) {
// Display a message dialog when the button is clicked
JOptionPane.showMessageDialog( window, "Button Clicked!" );
}
} );
button4.addActionListener( new ActionListener() {
@Override
public void actionPerformed( ActionEvent e ) {
// Display a message dialog when the button is clicked
JOptionPane.showMessageDialog( window, "Button Clicked!" );
}
} );
}
}
public class GraphicsPanel extends JPanel {
boolean drawing;
public GraphicsPanel() {
setPreferredSize( new Dimension( 500, 500 ) );
}
@Override
public void paintComponent( Graphics g ) {
Graphics2D g2 = ( Graphics2D ) g;
//1x10 board for the game:
int count = 0;
int[] board = new int[ 10 ];
for( int i = 0; i < board.length; i ++ ) {
g2.drawRect( 0 + count, 250, 50, 50 );
count += 50;
}
//Labels for each square 1-10:
g2.setFont( new Font( "TimesRoman", Font.PLAIN, 20 ) );
g2.drawString( "1", 20, 325 );
g2.drawString( "2", 70, 325 );
g2.drawString( "3", 120, 325 );
g2.drawString( "4", 170, 325 );
g2.drawString( "5", 220, 325 );
g2.drawString( "6", 270, 325 );
g2.drawString( "7", 320, 325 );
g2.drawString( "8", 370, 325 );
g2.drawString( "9", 420, 325 );
g2.drawString( "10", 465, 325 );
//Title and instructions for players:
g2.drawString( "1, 2, …, 10 Game", 175, 25 );
g2.drawString( "To Play: Players alternate placing either one or two pieces", 10, 60 );
g2.drawString( "on the leftmost open squares. In this game, we don’t", 30, 85 );
g2.drawString( "distinguish between players’ pieces, so we’ll call", 40, 110 );
g2.drawString( "them left and right. Left will go first.", 85, 135 );
g2.drawString( "The first player to place the tenth piece on the board wins!", 15, 170 );
//Labels for players buttons:
g2.drawString( "Player 1", 67, 385 );
g2.drawString( "Player 2", 367, 385 );
if( drawing ) {
g2.fillOval( 20, 20, 20, 20 );
}
}
public void setDrawing( boolean draw ) {
drawing = draw;
}
}