1
I have these buttons implemented with mouse click, only I wanted the following: that I could navigate through them direction keys up and down and enter through the enter. Does anyone know how it implements? It’s the buttons: btInitialize, btAbout and btSair.
I know it’s right that I have something done for you to help, but I’ve only done the ActionListener
buttons. Keyboard actions I really don’t know how to use.
Follow the code I have:
package ju;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Botoes extends JFrame {
private JPanel contentPane;
private JButton btIniciar;
private JButton btSobre;
private JButton btSair;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Botoes frame = new Botoes();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Botoes() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
btIniciar = new JButton("Iniciar");
btIniciar.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.out.println("foi clicado");
}
});
btIniciar.setBounds(146, 101, 125, 37);
contentPane.add(btIniciar);
btSobre = new JButton("Sobre");
btSobre.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("foi clicado");
}
});
btSobre.setBounds(146, 156, 125, 37);
contentPane.add(btSobre);
btSair = new JButton("Sair");
btSair.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
btSair.setBounds(145, 213, 126, 37);
contentPane.add(btSair);
}
}
I put it in my code, but it didn’t work (I ran it and it worked, but I only sent a sketch). Is it the way my buttons are? Take a look at what my code actually looks like.
package visao;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import controle.Constantes;
public class TelaInicial extends JFrame {
private JPanel painelTI;
private boolean seJogando;
private JButton btIniciarTI;
private JButton btSobreTI;
private JButton btSairTI;
private JLabel lblImgTelaInicialTI;
private JLabel lblTextNomeDoJogo;
public static int coluna=0;
JPanel p1;
public TelaInicial() {
setFocusable(true);
setUndecorated(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize( Constantes.ALTURA, Constantes.LARGURA);
setLocationRelativeTo(null);
painelTI = new JPanel();
painelTI.setLayout(null);
setContentPane(painelTI);
lblTextNomeDoJogo = new JLabel("");
lblTextNomeDoJogo.setForeground(Color.WHITE);
lblTextNomeDoJogo.setFont(GerenciarFonte.FontePlain(47));
lblTextNomeDoJogo.setBounds(242, 250, 633, 46);
painelTI.add(lblTextNomeDoJogo);
btIniciarTI = new JButton("Iniciar");
btIniciarTI.setFont(GerenciarFonte.FontePlain(44));
btIniciarTI.setForeground(Color.WHITE);
btIniciarTI.setFocusable(false);
btIniciarTI.setContentAreaFilled(false);
btIniciarTI.setFocusPainted(true);
btIniciarTI.setBounds(445, 350, 185, 47);
painelTI.add(btIniciarTI);
btSobreTI = new JButton("Sobre");
btSobreTI.setFont(GerenciarFonte.FontePlain(42));
btSobreTI.setForeground(Color.WHITE);//laranja 255 69 0
btSobreTI.setFocusable(false);
btSobreTI.setBorderPainted(true);
btSobreTI.setContentAreaFilled(false);
btSobreTI.setFocusPainted(true);
btSobreTI.setBounds(461, 414, 156, 41);
painelTI.add(btSobreTI);
btSairTI = new JButton("Sair");
btSairTI.setFont(GerenciarFonte.FontePlain(42));//RockoUltraFLF
btSairTI.setForeground(Color.WHITE);
btSairTI.setFocusable(false);
btSairTI.setBorderPainted(true);
btSairTI.setContentAreaFilled(false);
btSairTI.setFocusPainted(true);
btSairTI.setBounds(471, 472, 131, 37);
painelTI.add(btSairTI);
OrdenadorDeFoco of = new OrdenadorDeFoco(this);
of.configurarOrdem(btIniciarTI, btSobreTI, btSairTI);
lblImgTelaInicialTI = new JLabel();
lblImgTelaInicialTI.setIcon(new ImageIcon(getClass().getResource("/imagem/11.jpg")));
lblImgTelaInicialTI.setBounds(0, 0,Constantes.ALTURA, Constantes.LARGURA);
painelTI.add(lblImgTelaInicialTI);
setResizable(false);
setVisible(true);
}
public void paint(Graphics g){
super.paint(g);
g.setColor(Color.WHITE);
g.drawRect(436, 341, 201, 58);
g.drawRect(436, 408, 201, 47);
g.drawRect(436, 463, 201, 47);
}
public class Panel extends JGradientPanel {
private static final long serialVersionUID = 1L;
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(new Color(255,69,0));
g.draw3DRect( 414, 273, 180, 37, true );
g.draw3DRect( 414, 363, 180, 37, true );
g.draw3DRect( 414, 453, 180, 37, true );
}
public Panel(Color initialColor, Color finalColor) {
super(initialColor, finalColor);
}
}
public JButton getBtIniciarTI() {
return btIniciarTI;
}
public JButton getBtSairTI() {
return btSairTI;
}
public JButton getBtSobreTI() {
return btSobreTI;
}
public boolean isSeJogando() {
return seJogando;
}
public void setSeJogando(boolean seJogando) {
this.seJogando = seJogando;
}
}
package visao;
import java.awt.Font;
import java.awt.FontFormatException;
import java.io.IOException;
public class GerenciarFonte {
public Font carregarFonte(String caminho, int tipo, int tamanho) {
Font minhaFonte = null;
try {
minhaFonte = Font.createFont(Font.TRUETYPE_FONT, getClass().getResourceAsStream(caminho)).deriveFont(tipo,
tamanho);
} catch (IOException e) {
// TODO: handle exception
} catch (FontFormatException e) {
// TODO: handle exception
}
return minhaFonte;
}
public static Font FontePlain(int tamanho){
GerenciarFonte f = new GerenciarFonte();
Font cooper = f.carregarFonte("/fonte/cooper-black.ttf", Font.PLAIN, tamanho);
return cooper;
}
public static Font FonteBold(int tamanho){
GerenciarFonte f = new GerenciarFonte();
Font cooper = f.carregarFonte("/fonte/cooper-black.ttf", Font.BOLD, tamanho);
return cooper;
}
public static Font FontePlainListen(int tamanho){
GerenciarFonte f = new GerenciarFonte();
Font cooper = f.carregarFonte("/fonte/TypoSlabserif-Light.ttf", Font.BOLD, tamanho);
return cooper;
}
}
package visao;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import javax.swing.JPanel;
public class JGradientPanel extends JPanel {
private Color finalColor;
private Color initialColor;
public JGradientPanel(Color initialColor, Color finalColor) {
if (initialColor == null)
throw new IllegalArgumentException("Invalid initial color!");
if (finalColor == null)
throw new IllegalArgumentException("Invalid final color!");
this.initialColor = initialColor;
this.finalColor = finalColor;
}
public void setInitialColor(Color color) {
this.initialColor = color;
invalidate();
}
public void setFinalColor(Color color) {
this.finalColor = color;
invalidate();
}
public Color getInitialColor() {
return initialColor;
}
public Color getFinalColor() {
return finalColor;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
if (!isOpaque()) {
return;
}
GradientPaint paint = new GradientPaint(new Point2D.Float(getWidth() / 2, 0), initialColor,
new Point2D.Float(getWidth() / 2, getHeight()), finalColor);
g2d.setPaint(paint);
g2d.fillRect(0, 0, getWidth(), getHeight());
g2d.dispose();
}
}
public abstract class Constantes {
public static final int ALTURA = 1000;
public static final int LARGURA = 700;
}
this class focus sorter , using MVC, it should be on which layer?
Then add what you have done so far, in the form of [mcve], otherwise it is complicated to help. The answer below may not solve the problem, since keyevent only captures events and does not navigate. If you provide an example, it is easier to help.
– user28595
This is swing, javafx, android (think not, but go that) or something else?
– Victor Stafusa
This is swing. I put the code I have
– Gabriella
You only have the 3 buttons even on this screen? There are no other components not ne?
– user28595
Can’t be with TAB not? If it is with tab, you do not need to implement much. If it is with arrows, there is more complicated because it has to mess with Focus subsystem and it’s quite boring to implement.
– user28595
Without the class code
Constantes
,GerenciarFonte
andJGradientPanel
and the icon"/imagem/11.jpg"
it becomes more complicated to answer. I tried to kick the values of constants, comment on the lines ofGerenciarFonte
and the icon and replaceJGradientPanel
forJPanel
, but the result was just an empty gray screen.– Victor Stafusa
Ah, and by the way, the
OrdenadorDeFoco
is in the vision layer.– Victor Stafusa
Ah, I kicked higher values for the constants and the buttons appeared.
– Victor Stafusa
put the other classes
– Gabriella