gpt4 book ai didi

Java Swing 图形

转载 作者:行者123 更新时间:2023-12-01 13:10:43 42 4
gpt4 key购买 nike

我用 Java 编写了一个解决数独问题的代码,并使用 Java Applet 来设计它。现在,我尝试使用 Java Swing 使其看起来更好,并添加一些功能,例如“保存”数独板等。不幸的是,我对 Java Swing 不太熟悉,并且在使用 Graphics(绘制 9x9 的板)时遇到一些问题。我也不明白为什么我不能在同一个类中扩展 JFrame 和 JPanel 。

import java.applet.*;

import javax.swing.*;
import javax.swing.border.*;

import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import java.util.Scanner;

class Sudoku2 extends JPanel implements ActionListener, MouseListener,
ItemListener {

public JPanel createContentPane() {

JPanel GUI = new JPanel();
GUI.setLayout(null);

JPanel titlePanel = new JPanel();
titlePanel.setLayout(null);
titlePanel.setLocation(10, 0);
titlePanel.setSize(250, 30);
GUI.add(titlePanel);

JPanel scorePanel = new JPanel();
scorePanel.setLayout(null);
scorePanel.setLocation(10, 40);
scorePanel.setSize(250, 30);
GUI.add(scorePanel);

JPanel buttonPanel = new JPanel();
buttonPanel.setLayout(null);
buttonPanel.setLocation(0, 0);
buttonPanel.setSize(300, 200);
GUI.add(buttonPanel);

JButton solve = new JButton("Solve");
solve.setLocation(20, 100);
solve.setSize(100, 30);
buttonPanel.add(solve);

JButton clear_digit = new JButton("Clear Digit");
clear_digit.setLocation(20, 140);
clear_digit.setSize(100, 30);
buttonPanel.add(clear_digit);

GUI.setOpaque(true);
return GUI;
}

public JMenuBar createMenuBar() {

JMenuBar menuBar = new JMenuBar();

JMenu Menu = new JMenu("File");
menuBar.add(Menu);

JMenuItem new1 = new JMenuItem("New");
new1.addActionListener(this);
JMenuItem load = new JMenuItem("Load");
load.addActionListener(this);
JMenuItem save1 = new JMenuItem("Save");
save1.addActionListener(this);
JMenuItem save2 = new JMenuItem("Save As...");
save2.addActionListener(this);

Menu.add(new1);
Menu.add(load);
Menu.add(save1);
Menu.add(save2);

return menuBar;
}

private static void createAndShowGUI() {

JFrame frame = new JFrame("Sudoku Solver");

Sudoku2 demo = new Sudoku2();

frame.setJMenuBar(demo.createMenuBar());

frame.setContentPane(demo.createContentPane());

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(500, 500);

frame.setVisible(true);
}

public void paintComponent(Graphics g) {
super.paintComponent(g);

g.drawLine(50, 50, 100, 100);
}

@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub

}

public void actionPerformed(ActionEvent e) {

}

public void itemStateChanged(ItemEvent e) {

}

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();

}
});

}

}
`

最佳答案

您无法在一个类中扩展 JFrame 和 JPanel,因为 Java 不允许多重继承。

如果您不熟悉 Swing Graphic,您应该省去图形绘制的麻烦。

你可以做这样的事情来让你的生活更轻松。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SudokuBoard extends JPanel implements
ActionListener, MouseListener, ItemListener {

private JPanel board;
JTextField[] numberFields;

public SudokuBoard() {
super();
numberFields = new JTextField[81];
board = createContentPane();
}

public JPanel getBoard() {
return board;
}

private JPanel createContentPane() {

JPanel board = new JPanel();
board.setLayout(new GridLayout(9, 9, 1, 1));

for (int i = 0; i < numberFields.length; i++) {
numberFields[i] = new JTextField();
numberFields[i].setHorizontalAlignment(JTextField.CENTER);
numberFields[i].setFont(new Font("Ariel", Font.BOLD, 25));
numberFields[i].setText((i % 9)+1+"");

//set border
if ((i+1) % 3 == 0) {
numberFields[i].setBorder(BorderFactory.createMatteBorder(1, 1, 1, 5, Color.black));
}
if (i > 17 && i < 27) {
numberFields[i].setBorder(BorderFactory.createMatteBorder(1, 1, 5, 1, Color.black));
}
if (i > 44 && i < 54) {
numberFields[i].setBorder(BorderFactory.createMatteBorder(1, 1, 5, 1, Color.black));
}

board.add(numberFields[i]);
}

numberFields[20].setBorder(BorderFactory.createMatteBorder(1, 1, 5, 5, Color.black));
numberFields[23].setBorder(BorderFactory.createMatteBorder(1, 1, 5, 5, Color.black));
numberFields[26].setBorder(BorderFactory.createMatteBorder(1, 1, 5, 5, Color.black));
numberFields[47].setBorder(BorderFactory.createMatteBorder(1, 1, 5, 5, Color.black));
numberFields[50].setBorder(BorderFactory.createMatteBorder(1, 1, 5, 5, Color.black));
numberFields[53].setBorder(BorderFactory.createMatteBorder(1, 1, 5, 5, Color.black));

return board;
}

public JMenuBar createMenuBar() {

JMenuBar menuBar = new JMenuBar();

JMenu Menu = new JMenu("File");
menuBar.add(Menu);

JMenuItem new1 = new JMenuItem("New");
new1.addActionListener(this);
JMenuItem load = new JMenuItem("Load");
load.addActionListener(this);
JMenuItem save1 = new JMenuItem("Save");
save1.addActionListener(this);
JMenuItem save2 = new JMenuItem("Save As...");
save2.addActionListener(this);

Menu.add(new1);
Menu.add(load);
Menu.add(save1);
Menu.add(save2);

return menuBar;
}

private static void createAndShowGUI() {

JFrame frame = new JFrame("Sudoku Solver (Try Save)");
SudokuBoard demo = new SudokuBoard();

frame.setJMenuBar(demo.createMenuBar());
frame.setContentPane(demo.getBoard());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setVisible(true);
}

@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub

}

public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("Save")) {
int[][] numbers = new int[9][9];
int c = 0;
for(int i = 0; i < numbers.length; i++) {
for (int j = 0; j < numbers[i].length; j++) {

//check for non-int
numbers[i][j] = Integer.parseInt(numberFields[c].getText());

c++; //awesome!
}
}

for(int i = 0; i < numbers.length; i++) {
System.out.print("[");
for (int j = 0; j < numbers[i].length; j++) {
System.out.print(numbers[i][j]+" ");
}
System.out.println("]");
}

//save this 2-d array

System.out.println("Saving...");
}
}

public void itemStateChanged(ItemEvent e) {

}

public static void main(String[] args) {

SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}

关于Java Swing 图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22890999/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com