Domanda

, nonostante molti tentativi non riesco a ottenere il risultato che mi piacerebbe vedere - il testo centrato all'interno della JLabel e la JLabel un po 'centrato nel BorderLayout. Ho detto "un po '", perché ci dovrebbe essere anche un altro "status" etichetta nell'angolo in basso a destra della finestra. Ecco il pezzo di codice responsabile per questo:

setLayout(new BorderLayout());
JPanel area = new JPanel();
JLabel text = new JLabel(
        "<html>In early March, the city of Topeka," +
        " Kansas,<br>temporarily changed its name to Google..." +
        "<br><br>...in an attempt to capture a spot<br>" +
        "in Google's new broadband/fiber-optics project." +
        "<br><br><br>source: http://en.wikipedia.org/wiki/Google_server" +
        "#Oil_Tanker_Data_Center</html>", SwingConstants.CENTER);
text.setVerticalAlignment(SwingConstants.CENTER);
JLabel status = new JLabel("status", SwingConstants.SOUTH_EAST);
status.setVerticalAlignment(SwingConstants.CENTER);
Font font = new Font("SansSerif", Font.BOLD, 30);
text.setFont(font);
area.setBackground(Color.darkGray);
text.setForeground(Color.green);
// text.setAlignmentX(CENTER_ALIGNMENT);
// text.setAlignmentY(CENTER_ALIGNMENT);
// text.setHorizontalAlignment(JLabel.CENTER);
// text.setVerticalAlignment(JLabel.CENTER);
Font font2 = new Font("SansSerif", Font.BOLD, 20);
status.setFont(font2);
status.setForeground(Color.green);      
area.add(text, BorderLayout.CENTER);        
area.add(status, BorderLayout.EAST);
this.add(area);

Grazie per qualsiasi aiuto fornito.

È stato utile?

Soluzione

String text = "In early March, the city of Topeka, Kansas," + "<br>" +
              "temporarily changed its name to Google..." + "<br>" + "<br>" +
              "...in an attempt to capture a spot" + "<br>" +
              "in Google's new broadband/fiber-optics project." + "<br>" + "<br>" +"<br>" +
              "source: http://en.wikipedia.org/wiki/Google_server#Oil_Tanker_Data_Center";
JLabel label = new JLabel("<html><div style='text-align: center;'>" + text + "</div></html>");

Altri suggerimenti

Il seguente costruttore, JLabel(String, int) , consente di specificare l'allineamento orizzontale del marchio.

JLabel label = new JLabel("The Label", SwingConstants.CENTER);
myLabel.setHorizontalAlignment(SwingConstants.CENTER);
myLabel.setVerticalAlignment(SwingConstants.CENTER);

Se non è possibile ricostruire l'etichetta per qualche ragione, questo è il modo di modificare queste proprietà di un JLabel pre-esistente.

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