こんにちは、パネルに絵を描いてフレームに追加する際に問題があります。
import java.awt.Graphics;
import java.awt.Image;
import java.util.Random;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class CardComponent extends JPanel {
int x;int y;
String[] str = {
"D:\\Images\\ImageCards\\Clubs_Ace.png",
"D:\\Images\\ImageCards\\Clubs_King.png",
"D:\\Images\\ImageCards\\Clubs_Queen.png",
"D:\\Images\\ImageCards\\Clubs_Jack.png",
"D:\\Images\\ImageCards\\Clubs_10.png",
"D:\\Images\\ImageCards\\Clubs_9.png",
"D:\\Images\\ImageCards\\Clubs_8.png",
"D:\\Images\\ImageCards\\Clubs_7.png",
"D:\\Images\\ImageCards\\Clubs_6.png",
"D:\\Images\\ImageCards\\Clubs_5.png",
"D:\\Images\\ImageCards\\Clubs_4.png",
"D:\\Images\\ImageCards\\Clubs_3.png",
"D:\\Images\\ImageCards\\Clubs_2.png",
"D:\\Images\\ImageCards\\Diamonds_Ace.png",
"D:\\Images\\ImageCards\\Diamonds_King.png",
"D:\\Images\\ImageCards\\Diamonds_Queen.png",
"D:\\Images\\ImageCards\\Diamonds_Jack.png",
"D:\\Images\\ImageCards\\Diamonds_10.png",
"D:\\Images\\ImageCards\\Diamonds_9.png",
"D:\\Images\\ImageCards\\Diamonds_8.png",
"D:\\Images\\ImageCards\\Diamonds_7.png",
"D:\\Images\\ImageCards\\Diamonds_6.png",
"D:\\Images\\ImageCards\\Diamonds_5.png",
"D:\\Images\\ImageCards\\Diamonds_4.png",
"D:\\Images\\ImageCards\\Diamonds_3.png",
"D:\\Images\\ImageCards\\Diamonds_2.png",
"D:\\Images\\ImageCards\\Hearts_Ace.png",
"D:\\Images\\ImageCards\\Hearts_King.png",
"D:\\Images\\ImageCards\\Hearts_Queen.png",
"D:\\Images\\ImageCards\\Hearts_Jack.png",
"D:\\Images\\ImageCards\\Hearts_10.png",
"D:\\Images\\ImageCards\\Hearts_9.png",
"D:\\Images\\ImageCards\\Hearts_8.png",
"D:\\Images\\ImageCards\\Hearts_7.png",
"D:\\Images\\ImageCards\\Hearts_6.png",
"D:\\Images\\ImageCards\\Hearts_5.png",
"D:\\Images\\ImageCards\\Hearts_4.png",
"D:\\Images\\ImageCards\\Hearts_3.png",
"D:\\Images\\ImageCards\\Hearts_2.png",
"D:\\Images\\ImageCards\\Spades_Ace.png",
"D:\\Images\\ImageCards\\Spades_Ace.png",
"D:\\Images\\ImageCards\\Spades_King.png",
"D:\\Images\\ImageCards\\Spades_Queen.png",
"D:\\Images\\ImageCards\\Spades_Jack.png",
"D:\\Images\\ImageCards\\Spades_10.png",
"D:\\Images\\ImageCards\\Spades_9.png",
"D:\\Images\\ImageCards\\Spades_8.png",
"D:\\Images\\ImageCards\\Spades_7.png",
"D:\\Images\\ImageCards\\Spades_6.png",
"D:\\Images\\ImageCards\\Spades_5.png",
"D:\\Images\\ImageCards\\Spades_4.png",
"D:\\Images\\ImageCards\\Spades_3.png",
"D:\\Images\\ImageCards\\Spades_2.png"
};
Image img;
public CardComponent(int x,int y) {
setOpaque(false);
this.x = x;
this.y = y;
setSize(100,150);
setVisible(true);
}
public void paint(Graphics g) {
super.paint(g);
Random r = new Random();
String s = "";
s = str[r.nextInt(str.length)];
img = getToolkit().getImage(s);
g.drawImage(img,this.x , this.y, 100, 150, this);
}
public static void main(String[] args) {
CardComponent cc = new CardComponent(10,150);
JFrame jfrm = new JFrame();
jfrm.setSize(100, 150);
jfrm.setVisible(true);
jfrm.getContentPane().add(cc);
}
}
クラスupperが主な問題です(カードをペイントできません)。そして、これをメインフレームに追加しました(コードlower)。すべてのクラスにmainメソッドを追加して、正しく動作するかどうかを確認しました。java.awt.Button をインポートします。java.awt.Color をインポートします。import java.awt.event.ActionEvent; import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
public class PanelButtons extends JPanel{
Button but1;
Button but2;
Button but3;
JLabel jlab;
JSlider jsl;
public PanelButtons() {
setLayout(null);
Color c = new Color(0,120,0);
setSize(700,200);
setLocation(0,0);
setOpaque(false);
but1 = new Button("Check");
but1.setBackground(Color.CYAN);
but1.setLocation(660, 350);
but1.setBounds(683, 650, 70, 40);
add(but1);
but2 = new Button("Fold");
but2.setBackground(Color.CYAN);
but2.setLocation(660, 350);
but2.setBounds(753, 650, 70, 40);
add(but2);
but3 = new Button("Bet");
but3.setBackground(Color.CYAN);
but3.setLocation(660, 350);
but3.setBounds(823, 650, 70, 40);
add(but3);
int money = Player.PremierMoney();
jsl = new JSlider(0,1000);
jsl.setMajorTickSpacing(50);
jsl.setMinorTickSpacing(10);
jsl.setLabelTable(jsl.createStandardLabels(100));
jsl.setPaintTicks(true);
jsl.setPaintLabels(true);
jsl.setSize(400, 150);
jsl.setBackground(c);
jlab = new JLabel("Ваша ставка"+jsl.getValue());
jsl.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent ce) {
if(jsl.getValueIsAdjusting()) return;
jlab.setText("Ваша ставка:"+jsl.getValue());
}
});
jlab.setHorizontalTextPosition(JLabel.CENTER);
jlab.setVerticalAlignment(JLabel.BOTTOM);
jsl.setLocation(900,600);
jlab.setLocation(150,150);
add(jsl);
add(jlab);
but2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
but1.setEnabled(false);
but2.setEnabled(false);
but3.setEnabled(false);
}
});
}
public static void main(String[] args) {
JFrame jfrm = new JFrame("Some frame");
jfrm.setSize(200, 200);
PanelButtons pb = new PanelButtons();
jfrm.setContentPane(pb);
jfrm.setVisible(true);
jfrm.setDefaultCloseOperation(jfrm.EXIT_ON_CLOSE);
}
}
このクラスは、ボタンとスライダーを記述します。java.awt.Color をインポートします。javax.swing.JFrame をインポートします。
public class Images extends JFrame{
Images() {
// CardComponent cc = new CardComponent(530,550);
PanelButtons pb = new PanelButtons();
Color c = new Color(0,120,0);
// cc.setBounds(530, 550, 100, 150);
setSize(1366,750);
setBackground(c);
setContentPane(pb);
// setContentPane(cc);
setVisible(true);
}
public static void main(String[] args) {
Images im = new Images();
im.setResizable(false);
im.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
そして、これがパネルを追加するメイン フレームです。そのため、主な問題は、カードをどこにペイントするかです。手伝ってくれてありがとう。