开发者

JLabel, GridLayout

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

public class Gui extends JFrame
{
    priva开发者_JAVA技巧te JFrame window = new JFrame();
    private JButton but[] = new JButton[9];

    public Gui()
    {
        window.setSize(300,400);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        window.setLayout(new GridLayout(4, 3));

        JLabel txt = new JLabel("Will you dare?", JLabel.CENTER);
        txt.setLayout(new GridLayout(1, 1));
        txt.setHorizontalTextPosition(JLabel.CENTER);
        txt.setFont(new Font("Serif", Font.PLAIN, 21));
        window.add(txt);

        for(int i = 0; i < 9; i++)
        {
            but[i] = new JButton();
            window.add(but[i]);
        }  

        window.setVisible(true);

    }

    public void actionPerformed(ActionEvent e) 
    {

    }

}

I am Java noob, so, can you tell me, why "Will you dare" text doesn't appear in one line? Can you tell me how you found the solution, so other time I also will be able to do that by myself.


Wild guess here, but I'm thinking you want something like:

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

public class Gui extends JFrame
{
    private JFrame window = new JFrame();
    private JButton but[] = new JButton[9];

    public Gui()
    {
        window.setSize(300,400);
        window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//        window.setLayout(new GridLayout(4, 3));
        JPanel panel = new JPanel( new GridLayout(3, 3) );
        window.add(panel, BorderLayout.CENTER);

        JLabel txt = new JLabel("Will you dare?", JLabel.CENTER);
//        txt.setLayout(new GridLayout(1, 1));
        txt.setHorizontalTextPosition(JLabel.CENTER);
        txt.setFont(new Font("Serif", Font.PLAIN, 21));
//        window.add(txt);
        window.add(txt, BorderLayout.NORTH);

        for(int i = 0; i < 9; i++)
        {
            but[i] = new JButton();
//            window.add(but[i]);
            panel.add(but[i]);
        }

        window.setVisible(true);

    }

    public static void main(String args[]) throws Exception
    {
        new Gui();
    }

}


There are two caveats in your code :

you use the following expression to add components

 window.add(txt);
 //....
 window.add(but[i]);

both calls are shortcuts to

window.getContentPane().add

and the content pane of a JFrame has BorderLayout by default. And by default, this layout will add components in the center area if no other area/constraint is specified when adding. So you just put components one above the other, only the last will remain.

You should "cut"(poor translation, slice ?) more your GUI, adding your JLabel at center, and adding another JPanel on south, and inside it, use a flow layout and add your buttons.

Here is a good start for using the layout I mentionned, which are very standard.

Also you use a JLabel as a container. Almost all swing components inherit from java.awt.Container, this is used by swing to provide compound components but actually, you should never use them as container, i.e. not putting other components inside them or giving them a layout. You do that, giving it a layout at line

txt.setLayout(new GridLayout(1, 1));

so remove this line. Use swing components as components and JPanel (or JTabbedPane, etc.) as containers.

Regards, Stéphane btw, never hardcode values, use constants. No 9 but a BUTTON_COUNT = 9 static member. You could consider buying a java book. I used to recommend Deitel and Deitel to my students.


The problem here is quite simple: You are adding a layout to your label "txt".

You are saying that the label "txt" should take up the first row, first column of your JFrame. The first row, first column would result in a box. When you create a box like that you need to make sure the box is wide enough for your text to completely fit on a single line. In this case your text is simply jumping to the next line because there isn't enough room in that "box" you created for the entire label to fit on a single line.

Fixes to the problem:

  • Remove this line: txt.setLayout(new GridLayout(1, 1));

OR

  • Change the following line: txt.setLayout(new GridLayout(1, 1)); to txt.setLayout(new GridLayout(1,2));

You have one other problem that i noticed:

  • The following line should go at the top, beneath private JButton but[] = new JButton[9]; : JLabel txt = new JLabel("Will you dare?");//Removing the 'JLabel.CENTER'

That should do it.(i don't think i missed anything). :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜