我正在尝试在gui上绘制矩形,但没有显示
我尝试使用repaint();和blackJackPanel.add(repaint());这是没有道理的,但绝望的。有2个类别,它们是菜单页面,我不会发布轮盘赌课程,因为它太长了。一切正常,除了二十一点课程,我无法让那个方格出现。
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;
public class BlackJack extends JPanel implements ActionListener {
JPanel blackjackPanel = new JPanel();
JMenu menu = new JMenu("Gry");
JMenuItem menuRuletka = new JMenuItem("Ruletka");
JMenuItem mainMenu = new JMenuItem("MainMenu");
JMenuBar menuBar = new JMenuBar();
public void entireBlackJackQueue(){
panelConfig();
menuConfiguration();
}
public void panelConfig(){
blackjackPanel.setLayout(null);
}
public void menuConfiguration() {
menuBar.setBounds(0, 0, 40, 20);
menu.add(mainMenu);
menu.add(menuRuletka);
menuBar.add(menu);
blackjackPanel.add(menuBar);
}
@Override
public void actionPerformed(ActionEvent e) {
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawRect(230,80,10,10);
g.setColor(Color.RED);
g.fillRect(230,80,10,10);
}
}
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
public class MyFrame extends JFrame implements ActionListener {
JMenu menu = new JMenu("Gry");
JMenuItem menuRuletka = new JMenuItem("Ruletka");
JMenuItem menuBlackJack = new JMenuItem("BlackJack");
JFrame frame = new JFrame("Kasyno");
Ruletka ruletka = new Ruletka();
BlackJack blackJack = new BlackJack();
JMenuBar menuBar = new JMenuBar();
JPanel mainPanel = new JPanel();
Main main = new Main();
public MyFrame(){
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(900,600);
menuConfiguration();
mainFrameConfiguration();
ruletka.entireRuletkaQueue();
blackJack.entireBlackJackQueue();
frame.setContentPane(mainPanel);
frame.setResizable(false);
}
public void mainFrameConfiguration(){
mainPanel.setLayout(null);
mainPanel.add(menuBar);
}
public void menuConfiguration(){
menuBar.setBounds(0,0,40,20);
menuRuletka.addActionListener(this);
menuBlackJack.addActionListener(this);
menu.add(menuRuletka);
menu.add(menuBlackJack);
menuBar.add(menu);
ruletka.mainMenu.addActionListener(this);
blackJack.mainMenu.addActionListener(this);
blackJack.menuRuletka.addActionListener(this);
ruletka.menuBlackJack.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if(source==menuRuletka){
frame.remove(mainPanel);
frame.setContentPane(ruletka.ruletkaPanel);
frame.validate();
frame.repaint();
}
if(source==ruletka.mainMenu){
frame.setContentPane(mainPanel);
frame.remove(ruletka.ruletkaPanel);
frame.validate();
frame.repaint();
}
if(source==menuBlackJack){
frame.setContentPane(blackJack.blackjackPanel);
frame.remove(mainPanel);
frame.validate();
frame.repaint();
}
if(source==blackJack.mainMenu){
frame.setContentPane(mainPanel);
frame.remove(blackJack.blackjackPanel);
frame.validate();
frame.repaint();
}
if(source==blackJack.menuRuletka){
frame.setContentPane(ruletka.ruletkaPanel);
frame.remove(blackJack.blackjackPanel);
frame.validate();
frame.repaint();
}
if(source==ruletka.menuBlackJack){
frame.setContentPane(blackJack.blackjackPanel);
frame.remove(ruletka.ruletkaPanel);
frame.validate();
frame.repaint();
}
}
}
import java.awt.*;
public class Main {
int saldo=1000;
public void setSaldo(int saldo) {
this.saldo = saldo;
}
public int getSaldo() {
return saldo;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new MyFrame();
}
});
}
}
if that square will be able to apear that would be great!