我想显示新通知的数量,如下图所示。
根据您的理解,要显示此数字,每个操作都需要将 package swing22;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.*;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class MyFrame {
JFrame frame = new JFrame(" Test Frame ");
JPanel panel = new JPanel();
JButton button = new JButton("CLICK");
JLabel label = new JLabel(" 33 ");
public MyFrame(){
gui();
}
public void gui(){
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setBackground(Color.GREEN);
panel.add(button);
panel.add(label);
panel.add(new Surface());
frame.add(panel, BorderLayout.CENTER);
button.addActionListener(new Action());
frame.pack();
frame.setSize(300,300);
frame.setVisible(true);
frame.setResizable(false);
}
class Action implements ActionListener{
@Override
public void actionPerformed(ActionEvent arg0) {
label.setText("new value");
}
}
}
class Surface extends JPanel {
private void doDrawing(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g.setColor(Color.red);
g.drawOval(80, 80, 30, 30);
g.fillArc(140, 140, 30, 30, 0, 90);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
doDrawing(g);
}
}
传递给每个视图。除了将此功能分别添加到每个操作之外,还有其他方法吗?
答案 0 :(得分:0)
您应该使用视图编辑器:https://laravel.com/docs/master/views#view-composers
你的看起来可能是这样的:
public function __construct(NotificationsRepository $notifications)
{
// Dependencies automatically resolved by service container...
$this->notifications = $notifications;
}
public function compose(View $view)
{
$view->with('notificationsNumber', $this->notifications()->where('read',false)->get()->count());
}
然后将其包含在服务提供商中,并将其与menu
视图绑定。 Laravel将运行它,并在呈现$notificationsNumber
视图的任何时候添加menu