Related
i wrote a code using GridLayou, however i cant manage to delete the grey background and use the img i plugged as a background, and the picture demonstrate the problem:
Can someone please help?
this is the code
public class mainClass {
private static JButton start;
static BackgroundPanel bp = null;
static JFrame mainf = null;
static int R;
final static boolean shouldFill = true;
// group turns boolean
boolean redTurn = false;
boolean blueTurn = false;
boolean greenTurn = false;
boolean yellowTurn = false;
// boards
static ImageIcon gameBoard;
static ImageIcon blueBoard;
static ImageIcon qBoard;
// dice
static JButton dice_1 = null;
public static void main(String[] args) throws IOException {
mainf = new JFrame ("سين جيم");
// background
BufferedImage mFrame = ImageIO.read(new File("B1.png"));
bp = new BackgroundPanel(mFrame);
mainf.add(bp);
bp.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
// c.fill = GridBagConstraints.HORIZONTAL;
// Hi string
JLabel hi = new JLabel ("أهلا وسهلا بكم في لعبة الليدو");
Font fs = hi.getFont();
hi.setFont(fs.deriveFont(50f));
bp.add(hi);
// empty
// button
start = new JButton ( "لنبدأاللعب");
c.fill = GridBagConstraints.HORIZONTAL;
c.ipady = 20; //make this component tall
c.weightx = 0.0;
c.gridwidth = 3;
c.gridx = 0;
c.gridy = 1;
c.insets = new Insets(10,0,0,0); //top padding
bp.add(start, c);
// Action Listener
start.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
// starting the game area
// emptying all
bp.removeAll();
bp.revalidate();
bp.repaint();
BufferedImage mFrame2= null;
try {
// changing background
mFrame2 = ImageIO.read(new File("B2.png"));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// setting new background
bp = new BackgroundPanel(mFrame2);
bp.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
// adding gameBoard
gameBoard = new ImageIcon("gameBoard.png");
JLabel gameBoard_1 = new JLabel(gameBoard);
bp.add(gameBoard_1);
// adding blueBoard
blueBoard = new ImageIcon("blueBoard.png");
JLabel blueBoard_1 = new JLabel(blueBoard);
bp.add(blueBoard_1);
GridLayout experimentLayout = new GridLayout(4,4); // rows | , cols-
blueBoard_1.setLayout(experimentLayout);
// gameName panel
JPanel gameName = new JPanel();
gameName.setLayout(new GridLayout(1,0));
JLabel Ledu= new JLabel ("لعبة اللدو");
Font font3 = new Font("Verdana", Font.BOLD, 30);
Ledu.setFont(font3);
Ledu.setForeground(Color.WHITE);
gameName.add(Ledu);
// teamPanel
JPanel teamName = new JPanel();
teamName.setLayout(new GridLayout(2,0));
JLabel blue= new JLabel ("الفريق الأرزق");
Font font2 = new Font("Verdana", Font.BOLD, 20);
blue.setFont(font2);
blue.setForeground(Color.BLUE);
JLabel red= new JLabel ("الفريق الأحمر");
red.setFont(font2);
red.setForeground(Color.RED);
JLabel yellow= new JLabel ("الفريق الأصفر");
yellow.setFont(font2);
yellow.setForeground(Color.YELLOW);
JLabel green= new JLabel ("الفريق الأخضر");
green.setFont(font2);
green.setForeground(Color.GREEN);
// team panel
teamName.add(blue);
teamName.add(red);
teamName.add(yellow);
teamName.add(green);
// adding question
JPanel question = new JPanel();
question.setLayout(new GridLayout(3,2));
ImageIcon q = new ImageIcon("Q.png");
JLabel q_1 = new JLabel(q);
// adding Question panel
question.add(q_1);
// dicePanel
final JPanel dicePanel = new JPanel();
dicePanel.setLayout(new GridLayout(4,0));
ImageIcon dice = new ImageIcon("dice.png");
dice_1 = new JButton(dice);
// adding dice panel
dicePanel.add(dice_1);
dice_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
dicePanel.remove(dice_1);
dicePanel.revalidate();
dicePanel.repaint();
ImageIcon dice = new ImageIcon("dice.gif");
dice_1 = new JButton(dice);
dicePanel.add(dice_1);
// random number
Random r = new Random();
R = r.nextInt(10) + 2;
System.out.println(R);
}
});
// Centeralizing Ledu
Ledu.setHorizontalAlignment(SwingConstants.CENTER);
gameName.setAlignmentX(Component.CENTER_ALIGNMENT);
blue.setHorizontalAlignment(SwingConstants.CENTER);
red.setHorizontalAlignment(SwingConstants.CENTER);
yellow.setHorizontalAlignment(SwingConstants.CENTER);
green.setHorizontalAlignment(SwingConstants.CENTER);
teamName.setAlignmentX(Component.CENTER_ALIGNMENT);
// no backgrounf color
Ledu.setOpaque( false );
gameName.setOpaque( false );
blue.setOpaque( false );
red.setOpaque( false );
yellow.setOpaque( false );
green.setOpaque( false );
teamName.setOpaque( false );
question.setOpaque( false );
dicePanel.setOpaque( false );
// final add
blueBoard_1.add(gameName, BorderLayout.PAGE_START);
blueBoard_1.add(teamName, BorderLayout.CENTER);
blueBoard_1.add(question, BorderLayout.LINE_END);
blueBoard_1.add(dicePanel, BorderLayout.PAGE_END);
/*
// add dice
ImageIcon dice = new ImageIcon("dice.png");
dice_1 = new JButton(dice);
dice_1.setSize(100, 70);
bp.add(dice_1);
// changin to moving dice
dice_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
bp.remove(dice_1);
bp.revalidate();
bp.repaint();
ImageIcon dice = new ImageIcon("dice.gif");
dice_1 = new JButton(dice);
dice_1.setSize(100, 70);
bp.add(dice_1);
// random number
Random r = new Random();
int R = r.nextInt(10) + 2;
System.out.println(R);
}
});
*/
mainf.setContentPane(bp);
};}); // end of start actionListener
mainf.pack();
mainf.setVisible(true);
};
// dice class
}
A JPanel is opaque by default so you see a grey background.
You need to use:
panel.setOpaque( false );
if you add a panel to your component containing the background image.
By the way you should NOT be using static variables. Look at the examples from the Swing tutorial for a better way to create your frames. That is create a class to represent your game. This would be a JPanel. Then you define all the variables you need for you game in this class. Then you simple add the panel to the frame. You should NOT be createing components in the main() method.
I know there are plenty of BoxLayout questions on here, however I can't find one that fixes my problem. I need my scoreDescPanel to show each label directly below each other (like a list) however I'm having problems with BoxLayout. The priblem occurs on the line scoreDescPanel.add(lblScoreDesc[i]); at the bottom.
private JFrame frame;
private JPanel panel;
private JPanel dicePanel;
private JButton btnRoll;
private JButton[] btnDice = new JButton[5];
private JPanel mainPanel;
private JPanel scoreDescPanel;
private JPanel scoreBtnPanel;
private JLabel[] lblScoreDesc = new JLabel[20];
private JButton[] btnScore = new JButton[20];
private Yahtzee y = new Yahtzee();
public YahtzeeGUI(){
createWindow();
addButtonRoll();
addButtonDice();
addMainPanel();
addScoreDesc();
//addScoreCardUpper();
//addScoreCardLower();
frame.add(panel);
frame.setVisible(true);
}
public void createWindow(){
frame = new JFrame();
frame.setTitle("Yahtzee");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1000,700);
panel = new JPanel(new BorderLayout());
dicePanel = new JPanel();
mainPanel = new JPanel();
scoreDescPanel = new JPanel();
}
public void addButtonRoll(){
btnRoll = new JButton ("Roll the Dice");
btnRoll.addActionListener(new RollHandler());
dicePanel.add (btnRoll);
panel.add(dicePanel, BorderLayout.SOUTH);
}
public void addButtonDice(){
for (int i = 0; i < btnDice.length; i++){
btnDice[i] = new JButton(String.valueOf(y.dice[i].getFaceValue()));
btnDice[i].addActionListener(new HoldHandler());
dicePanel.add (btnDice[i]);
}
panel.add(dicePanel, BorderLayout.SOUTH);
}
public void addMainPanel(){
mainPanel.setLayout(new BorderLayout());
mainPanel.setBackground(Color.red);
panel.add(mainPanel, BorderLayout.CENTER);
}
public void addScoreDesc(){
lblScoreDesc[0] = new JLabel("UPPER SECTION");
lblScoreDesc[1] = new JLabel("Aces");
lblScoreDesc[2] = new JLabel("Twos");
lblScoreDesc[3] = new JLabel("Threes");
lblScoreDesc[4] = new JLabel("Fours");
lblScoreDesc[5] = new JLabel("Fives");
lblScoreDesc[6] = new JLabel("Sixes");
lblScoreDesc[7] = new JLabel("TOTAL SCORE");
lblScoreDesc[8] = new JLabel("BONUS");
lblScoreDesc[9] = new JLabel("TOTAL UPPER");
lblScoreDesc[10] = new JLabel("LOWER SECTION");
lblScoreDesc[11] = new JLabel("3 of a Kind");
lblScoreDesc[12] = new JLabel("4 of a Kind");
lblScoreDesc[13] = new JLabel("Full House");
lblScoreDesc[14] = new JLabel("Small Straight");
lblScoreDesc[15] = new JLabel("Large Straight");
lblScoreDesc[16] = new JLabel("Yahtzee!");
lblScoreDesc[17] = new JLabel("Chance");
lblScoreDesc[18] = new JLabel("TOTAL LOWER");
lblScoreDesc[19] = new JLabel("GRAND TOTAL");
mainPanel.add(scoreDescPanel, BorderLayout.WEST);
scoreDescPanel.setLayout(new BoxLayout(mainPanel,BoxLayout.Y_AXIS));
for(int i = 0; i < lblScoreDesc.length; i++){
scoreDescPanel.add(lblScoreDesc[i]);
}
}
BoxLayout doesn't allow a different target container from that on which the layout is being set, i.e.
scoreDescPanel.setLayout(new BoxLayout(mainPanel,BoxLayout.Y_AXIS));
should be
scoreDescPanel.setLayout(new BoxLayout(scoreDescPanel, BoxLayout.Y_AXIS));
Read: How to Use BoxLayout
Hi i have a trouble in my program because i need to set an array for easy way of incrementing it because it is sales so i declare of my array like this iam not yet done of my program this is my program so far .
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class We extends JFrame
{
public static JPanel panel2 = new JPanel();
public static String totallist,addprod;
public static int grandtotal,ve,xxx,z,x,adding,pay,totalp,totalc,payment;
public static JTextField in = new JTextField(z);
public static JTextField ki = new JTextField(15);
public static double disc,totalbayad,sukli;
public static int benta[] = new int [11];
public static String prod[] = new String[11];
public static void main(String[] args)
{
We frameTabel = new We();
prod[1] = "Palmolive";
prod[2] = "Egg";
prod[3] = "Milo";
prod[4] = "Noodles";
prod[5] = "PancitCanton";
prod[6] = "CornBeef";
prod[7] = "LigoSardines";
prod[8] = "CokeSakto";
prod[9] = "RcBig";
prod[10] = "GibsonLespaulGuitar";
benta[1] = 6;
benta[2] = 5;
benta[3] = 6;
benta[4] = 9;
benta[5] = 10;
benta[6] = 25;
benta[7] = 16;
benta[8] = 6;
benta[9] = 16;
benta[10] = 14000;
}
JFrame frame = new JFrame("Customer");
JFrame prodcho = new JFrame("Unofficial receipt");
JFrame want = new JFrame("Buy AGain");
JFrame ftinda = new JFrame("Item && Prices");
JButton blogin = new JButton("Login");
JPanel panel = new JPanel();
JTextField txuser = new JTextField(15);
JPasswordField pass = new JPasswordField(15);
JLabel lab = new JLabel("Username :");
JLabel pas = new JLabel("Password :");
JLabel cos;
//JPanel panel = new JPanel();
JButton y1;
JButton y2;
We()
{
super("Enter Your Account !");
setSize(300,200);
setLocation(500,280);
panel.setLayout (null);
txuser.setBounds(90,30,150,20);
pass.setBounds(90,65,150,20);
blogin.setBounds(110,100,80,20);
lab.setBounds(15,28,150,20);
pas.setBounds(15,63,150,20);
panel.add(lab);
panel.add(pas);
panel.add(blogin);
panel.add(txuser);
panel.add(pass);
getContentPane().add(panel);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
actionlogin();
}
public void actionlogin()
{
blogin.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
String puname = txuser.getText();
String ppaswd = pass.getText();
if(puname.equals("vincent") && ppaswd.equals("puge"))
{
setVisible(false);
JPanel panel1 = new JPanel();
frame.setVisible(true);
frame.setSize(300,200);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout());
cos = new JLabel("Do you have a Customer ?");
y1 = new JButton("Yes");
y2 = new JButton("No");
panel1.setLayout(null);
cos.setBounds(70,30,150,20);
y1.setBounds(80,65,150,20);
y2.setBounds(140,65,150,20);
y1.setSize(55,30);
y2.setSize(55,30);
panel1.add(y1);
panel1.add(y2);
panel1.add(cos);
frame.add(panel1);
y1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
Object source = ae.getSource();
if(source == y1)
{
frame.setVisible(false);
//--------------------------------------
int boundsStart=10;
panel.l2.add(new JLabel("-Product-").setBounds(20,boundsStart,150,20));
boundsStart+=20;
for (int i=1; i<11; i++)
{
panel2.add(new JLabel(i+"."+prod[i]).setBounds(20,boundsStart,150,20));
boundsStart+=20;
}
boundsStart = 30; //reset bounds counter
for (int i=1; i<11; i++)
{
panel2.add(new JLabel(""+benta[i]).setBounds(20,boundsStart,150,20));
boundsStart+=20;
}
//You could then change the other JLabels that came after this point in the same way I just did
//price,ent,law, and qx
//--------------------------------------
JLabel vince = new JLabel("-Product-");
JLabel l1 = new JLabel("1."+prod[1]);
JLabel l2 = new JLabel("2."+prod[2]);
JLabel l3 = new JLabel("3."+prod[3]);
JLabel l4 = new JLabel("4."+prod[4]);
JLabel l5 = new JLabel("5."+prod[5]);
JLabel l6 = new JLabel("6."+prod[6]);
JLabel l7 = new JLabel("7."+prod[7]);
JLabel l8 = new JLabel("8."+prod[8]);
JLabel l9 = new JLabel("9."+prod[9]);
JLabel l10 = new JLabel("10."+prod[10]);
JLabel p1 = new JLabel(""+benta[1]);
JLabel p2 = new JLabel(""+benta[2]);
JLabel p3 = new JLabel(""+benta[3]);
JLabel p4 = new JLabel(""+benta[4]);
JLabel p5 = new JLabel(""+benta[5]);
JLabel p6 = new JLabel(""+benta[6]);
JLabel p7 = new JLabel(""+benta[7]);
JLabel p8 = new JLabel(""+benta[8]);
JLabel p9 = new JLabel(""+benta[9]);
JLabel p10 = new JLabel(""+benta[10]);
JLabel price = new JLabel("-Price-");
JButton ent = new JButton("Enter");
JLabel law = new JLabel("Enter No. of Product");
JLabel qx = new JLabel("Enter Quantity");
ftinda.setVisible(true);
ftinda.setSize(350,350);
ftinda.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ftinda.setLayout(new GridLayout());
panel2.setLayout(null);
vince.setBounds(20,10,150,20);
l1.setBounds(20,30,150,20);
l2.setBounds(20,50,150,20);
l3.setBounds(20,70,150,20);
l4.setBounds(20,90,150,20);
l5.setBounds(20,110,150,20);
l6.setBounds(20,130,150,20);
l7.setBounds(20,150,150,20);
l8.setBounds(20,170,150,20);
l9.setBounds(20,190,150,20);
l10.setBounds(20,210,150,20);
p1.setBounds(230,30,150,20);
p2.setBounds(230,50,150,20);
p3.setBounds(230,70,150,20);
p4.setBounds(230,90,150,20);
p5.setBounds(230,110,150,20);
p6.setBounds(230,130,150,20);
p7.setBounds(230,150,150,20);
p8.setBounds(230,170,150,20);
p9.setBounds(230,190,150,20);
p10.setBounds(230,210,150,20);
price.setBounds(225,10,150,20);
in.setBounds(150,250,150,20);
law.setBounds(20,253,150,20);
qx.setBounds(20,280,150,20);
ki.setBounds(150,280,150,20);
ent.setBounds(220,250,150,20);
in.setSize(42,20);
ki.setSize(42,20);
ent.setSize(65,50);
panel2.add(vince);
panel2.add(l1);
panel2.add(l2);
panel2.add(l3);
panel2.add(l4);
panel2.add(l5);
panel2.add(l6);
panel2.add(l7);
panel2.add(l8);
panel2.add(l9);
panel2.add(l10);
panel2.add(p1);
panel2.add(p2);
panel2.add(p3);
panel2.add(p4);
panel2.add(p5);
panel2.add(p6);
panel2.add(p7);
panel2.add(p8);
panel2.add(p9);
panel2.add(p10);
panel2.add(price);
panel2.add(in);
panel2.add(law);
panel2.add(ent);
panel2.add(qx);
panel2.add(ki);
ftinda.add(panel2);
ent.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ae)
{
ftinda.setVisible(false);
JPanel panel3 = new JPanel();
JLabel cos1 = new JLabel("Do you want to buy more ?");
JButton yy = new JButton("Yes");
JButton nn = new JButton("No");
panel3.setLayout(null);
cos1.setBounds(70,30,150,20);
yy.setBounds(80,65,150,20);
nn.setBounds(140,65,150,20);
yy.setSize(55,30);
nn.setSize(55,30);
panel3.add(cos1);
panel3.add(yy);
panel3.add(nn);
want.add(panel3);
want.setVisible(true);
want.setSize(300,200);
want.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
want.setLayout(new GridLayout());
addprod = prod[];
adding = benta[];
totalp =
totalc = totalp;
totallist = totallist + addprod +"" +x+ "pcs = "+totalc+"pesos";
nn.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent ea)
{
Object source1 = ea.getSource();
{
if(source1 == nn)
{
JPanel panel4 = new JPanel();
panel4.setLayout(null);
prodcho.setVisible(true);
prodcho.setSize(300,200);
prodcho.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
prodcho.setLayout(new GridLayout());
prodcho.add(panel4);
}
}
}
});
}
});
}
}
});
}
else
{
JOptionPane.showMessageDialog(null,"Wrong Password / Username");
txuser.setText("");
pass.setText("");
txuser.requestFocus();
}
}
});
}
}
As PeterMmm stated arrayLists should be able to solve your predicament. ArrayLists allow you to make an array of any type very easily and has a very nice interface such as
mylist.add(element)
Maroun Maroun also made a valid point that you can approach this much more nicely using a loop so that you can avoid the large amount of repitition in your code, but its not necessary to do so if you are happy with it.
Hopefully the following sample of using ArrayList can help you:
http://javarevisited.blogspot.de/2011/05/example-of-arraylist-in-java-tutorial.html
And if you want more information about arraylist here is the docs:
http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
My last tip, while looking at your code is to add an actionListener to your button, as currently it does nothing ;)
ent.addActionListener(new ActionListener()
{
#Override
public void actionPerformed( ActionEvent e )
{
//..do stuff here or call a function to do stuff :)
}
});
You might want to check out putting this into a bigger loop. You could set up a loop and then use generic statements to add Labels and size them instead of adding a different Label object for each one. It would cut about 20 lines of code.
EDIT: By loops I was talking about how to deal with setting bounds and adding to the panel. An example would be something like
while(condition)
{
panel.add(new JLabel(information).setBounds(bounds));
}
This gets rid of the repetition of having code like:
JLabel p1 = new JLabel(info);
JLabel p2 = new JLabel(info2);
...
panel1.add(p1);
panel1.add(p2);
...
p1.setBounds(bounds1);
p2.setBounds(bounds2);
...
I have designed a frame containing a few controls using the design view in Netbeans 6.9.1. Further, I have added an empty panel in which I am trying to toggle display of a couple of swing components on button click. The problem is that on button click, the panel displays nothing. The code is as follows:
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JPanel txtPanel = new JPanel();
JPanel listPanel = new JPanel();
JTextField txtfield = new JTextField("ABCDEFGHIJ", 20);
txtPanel.add(txtfield);
JList<String> list = new JList<String>();
DefaultListModel<String> model = new DefaultListModel<String>();
for (int i = 0; i < userCommands.size(); i++){
model.addElement(userCommands.get(i));
}
list.setModel(model);
listPanel.add(list);
jPanel2.add(listPanel, "list");
jPanel2.add(txtPanel, "text");
//MainUI.getFrames()[0].add(jPanel2, BorderLayout.CENTER);
itemStateChanged("text");
}
Code for itemStateChanged is as follows:
public void itemStateChanged(String disp) {
CardLayout cl = (CardLayout)(jPanel2.getLayout());
cl.show(jPanel2, disp);
}
In the first piece of code, jPanel2 is dragged and dropped onto the frame containing other components, what i am trying to achieve here is that on button click, the jPanel2 should toggle between text field and list. But currently, the panel is not displaying anything on button click.
Before even considering if jPanel2 can switch between the different panels (the different cards), is jPanel2 on display at all anywhere? The only code I can see that displayes the jPanel2 is adding it to the MainUI but it is commented out?! So how do you know that the display inside jPanel2 isn't switching?
I did a quick example code for you .Have a look.You need to define the Card layout as global and try.
import javax.swing.AbstractAction;
public class TestPanel extends JPanel {
/**
* Create the panel.
*/
JPanel panel;
CardLayout cl = new CardLayout();
public TestPanel() {
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{0, 155, 0, 0};
gridBagLayout.rowHeights = new int[]{94, 0, 0};
gridBagLayout.columnWeights = new double[]{0.0, 0.0, 1.0, Double.MIN_VALUE};
gridBagLayout.rowWeights = new double[]{1.0, 1.0, Double.MIN_VALUE};
setLayout(gridBagLayout);
JButton btnNewButton = new JButton("New button");
GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
gbc_btnNewButton.insets = new Insets(0, 0, 5, 5);
gbc_btnNewButton.gridx = 0;
gbc_btnNewButton.gridy = 0;
add(btnNewButton, gbc_btnNewButton);
btnNewButton.setAction(new AbstractAction("New button") {
#Override
public void actionPerformed(ActionEvent arg0) {
JPanel txtPanel = new JPanel();
JPanel listPanel = new JPanel();
JTextField txtfield = new JTextField("ABCDEFGHIJ", 20);
txtPanel.add(txtfield);
JList<String> list = new JList<String>();
DefaultListModel<String> model = new DefaultListModel<String>();
for (int i = 0; i < 3; i++){
model.addElement("Sanjaya");
}
list.setModel(model);
listPanel.add(list);
panel.add(listPanel, "list");
panel.add(txtPanel, "text");
//MainUI.getFrames()[0].add(jPanel2, BorderLayout.CENTER);
itemStateChanged("list");
}
});
JTextArea textArea = new JTextArea();
textArea.setLineWrap(true);
GridBagConstraints gbc_textArea = new GridBagConstraints();
gbc_textArea.insets = new Insets(0, 0, 5, 5);
gbc_textArea.fill = GridBagConstraints.BOTH;
gbc_textArea.gridx = 1;
gbc_textArea.gridy = 0;
add(textArea, gbc_textArea);
panel = new JPanel(cl);
GridBagConstraints gbc_panel = new GridBagConstraints();
gbc_panel.insets = new Insets(0, 0, 5, 0);
gbc_panel.fill = GridBagConstraints.BOTH;
gbc_panel.gridx = 2;
gbc_panel.gridy = 0;
add(panel, gbc_panel);
}
public void itemStateChanged(String disp) {
cl.show(panel, disp);
}
}
hii every one,
following is my code which displays 4 panel
one is at NORTH,....WEST , SOUTH
i want to display am image at EAST at container
how is it possible?
public class ImageProcessor extends JApplet {
JPanel panel1,panel2,panel3,panel4,panel5;
JTextField nameTxt,addTxt,phoneTxt,emailTxt;
JButton capture,download,cancle,sendEmail;
JLabel head,name,add,phone,email;
//function to align components using gridBagLayOut..
private GridBagConstraints getConstraints(int gridx, int gridy,int gridwidth, int gridheight, int anchor)
{
GridBagConstraints c =new GridBagConstraints();
c.insets = new Insets(10,10,10,10);
c.ipadx = 0;
c.ipady = 0;
c.gridx = gridx;
c.gridy = gridy;
c.gridwidth = gridwidth;
c.gridheight = gridheight;
c.anchor = anchor;
return c;
}
//ends here...
public void init() {
panel1 = new JPanel();
panel2 = new JPanel();
panel3 = new JPanel();
panel4 = new JPanel();
panel5 = new JPanel();
nameTxt = new JTextField(20);
addTxt = new JTextField(20);
phoneTxt = new JTextField(20);
emailTxt = new JTextField(20);
capture = new JButton("capture");
download = new JButton("download");
sendEmail = new JButton("sendEmail");
head = new JLabel("BUSINESS CARD READER");
name = new JLabel("NAME:");
add = new JLabel("ADDRESS:");
phone = new JLabel("PHONE:");
email = new JLabel("EMAIL:");
Container myPane = getContentPane();
myPane.setLayout(new BorderLayout());
panel1.setLayout(new BorderLayout());
panel2.setLayout(new GridBagLayout());
panel2.add(head,getConstraints(0,0,1,1,GridBagConstraints.CENTER));
panel3.setLayout(new FlowLayout());
panel3.add(capture);
panel3.add(download);
panel3.add(sendEmail);
panel4.setLayout(new GridBagLayout());
panel4.add(name,getConstraints(0,0,1,1,GridBagConstraints.CENTER));
panel4.add(nameTxt,getConstraints(1,0,1,1,GridBagConstraints.CENTER));
panel4.add(add,getConstraints(0,1,1,1,GridBagConstraints.CENTER));
panel4.add(addTxt,getConstraints(1,1,1,1,GridBagConstraints.CENTER));
panel4.add(phone,getConstraints(0,2,1,1,GridBagConstraints.CENTER));
panel4.add(phoneTxt,getConstraints(1,2,1,1,GridBagConstraints.CENTER));
panel4.add(email,getConstraints(0,3,1,1,GridBagConstraints.CENTER));
panel4.add(emailTxt,getConstraints(1,3,1,1,GridBagConstraints.CENTER));
panel1.add(panel2,BorderLayout.NORTH);
panel1.add(panel3,BorderLayout.SOUTH);
panel1.add(panel4,BorderLayout.WEST);
panel1.add(panel5,BorderLayout.EAST);
setSize(500,500);
myPane.add(panel1,BorderLayout.CENTER);
}
public void start(){
this.setSize(800,500);
}
}
Create a JLabel without text ("") and use setIcon to set the image to be displayed.
Here is an example.
Add the picture on a JButton or JLabel to your panel5:
JButton buttonForPicture = new JButton();
buttonForPicture.setBorder(new EmptyBorder(0, 0, 0, 0));
buttonForPicture.setOpaque(false);
buttonForPicture.setIcon(new ImageIcon(imageFilePath));
panel5.add(buttonForPicture);