Domanda

I'm writing a quick weatherapplication using gridbaglayout for the layout manager. However gridbag is refusing to put the JLabel objects into the seperate cells defined, rather putting all of them into one cell.

public class WeatherApp extends JFrame {

private JLabel jDayOfTheWeek;
RSSReader rss = null;
private JLabel jWindDirectionToday;
private JLabel jWindSpeed1;
private JLabel jMaxTemp1;
private JLabel jMinTemp1;
private JLabel jVisibility1;
private JLabel jPressure1;
private JLabel jHumidity1;
private JLabel jWindSpeed2;
private JLabel jMaxTemp2;
private JLabel jMinTemp2;
private JLabel jVisibility2;
private JLabel jPressure2;
private JLabel jHumidity2;
private JLabel jWindSpeed3;
private JLabel jMaxTemp3;
private JLabel jMinTemp3;
private JLabel jVisibility3;
private JLabel jPressure3;
private JLabel jHumidity3;

public WeatherApp() {


    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setTitle("Detailed weather");
    this.setSize(320,480);
    GridBagLayout myLayout = new GridBagLayout();
    JPanel mainFrame = new JPanel(myLayout);
    rss = new RSSReader();
    GridBagConstraints c = new GridBagConstraints();



    c.gridwidth = 4;
    c.gridheight = 10;

    jDayOfTheWeek = new JLabel("lelel");
    jDayOfTheWeek.setText(this.getDayOfTheWeek());
    c.anchor = GridBagConstraints.FIRST_LINE_START;
    c.gridx = 0;
    c.gridy = 0;

    c.weightx = 1;
    c.weighty = 1;


    mainFrame.add(jDayOfTheWeek, c);



    jWindDirectionToday = new JLabel(rss.getFirstDay().getWindDirection());
     c.anchor = GridBagConstraints.CENTER;
    c.gridx = 2;
    c.gridy = 2;
     c.weightx = 1;
    c.weighty = 1;
    mainFrame.add(jWindDirectionToday, c);


    jWindSpeed1 = new JLabel(Integer.toString(rss.getFirstDay().getWindSpeed()) + "mph");
    c.gridx = 3;
    c.gridy = 3;
     c.weightx = 1;
    c.weighty = 1;
    mainFrame.add(jWindSpeed1, c);


    jMaxTemp1 = new JLabel(Integer.toString(rss.getFirstDay().getMaxTemp()) + "C");
    c.gridx = 4;
    c.gridy = 4;
     c.weightx = 1;
    c.weighty = 1;
    mainFrame.add(jMaxTemp1, c);



    this.add(mainFrame);



}

public String getDayOfTheWeek() {
    Calendar c = Calendar.getInstance(TimeZone.getDefault());

    Date currentDate = c.getTime();
    return new SimpleDateFormat("EEEE").format(currentDate);



}

public static void main(String[] args) {
    WeatherApp myapp = new WeatherApp();
    myapp.setVisible(true);

}

Now if you look at the code, the JLabels have been assigned to different grid positions: 0,0 2,2 3,3 and 4,4 however the result when I compile and execute all grid positions are ignored, however if I use the constants for gridbagconstraints when anchoring those are followed.

È stato utile?

Soluzione

Remove next lines and your labels will be in correct positions:

c.gridwidth = 4;
c.gridheight = 10;

Altri suggerimenti

I also had this problem, however the chosen solution did not solve my issue. After much research and painstaking trial-and-error, I also figured out that this can be caused by putting the same object in multiple cells.

Apparently, GridBagLayout requires each cell to have its own individual object. Otherwise, only the first cell will be filled (since the object was new to the GridBagLayout at the time).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top